2015-11-01 57 views
0

所以基本上我有一个blade.php,控制器页面和表单请求页面(验证)。我试图保持我的模态对话框打开,如果有错误,但我不能弄清楚,我错过了哪些代码或需要改变?验证错误后保持模态对话框打开laravel

blade.php

<div id="register" class="modal fade" role="dialog"> 
... 

<script type="text/javascript"> 
if ({{ Input::old('autoOpenModal', 'false') }}) { 
    //JavaScript code that open up your modal. 
    $('#register').modal('show'); 
} 
</script> 

Controller.php这样

class ManageAccountsController extends Controller 
{ 
    public $userRepository; 

    public function __construct(UserRepository $userRepository) 
    { 
     $this->userRepository = $userRepository; 
    } 

    public function index() 
    { 
     $users = User::orderBy('name')->get(); 
     $roles = Role::all(); 

     return view('manage_accounts', compact('users', 'roles')); 
    } 

    public function register(StoreNewUserRequest $request) 
    { 
     // process the form here 
     $this->userRepository->upsert($request); 
     Session::flash('flash_message', 'User successfully added!'); 

     //$input = Input::except('password', 'password_confirm'); 
     //$input['autoOpenModal'] = 'true'; //Add the auto open indicator flag as an input. 

     return redirect()->back(); 
    } 
} 

class UserRepository { 

    public function upsert($data) 
    { 

      // Now we can separate this upsert function here 
     $user = new User; 
     $user->name  = $data['name']; 
     $user->email = $data['email']; 
     $user->password = Hash::make($data['password']); 
     $user->mobile = $data['mobile']; 
     $user->role_id = $data['role_id']; 

      // save our user 
     $user->save(); 

     return $user; 
    } 
} 

request.php

class StoreNewUserRequest extends Request 
{ 
    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     // create the validation rules ------------------------ 

     return [ 
     'name'    => 'required',      // just a normal required validation 
     'email'   => 'required|email|unique:users',  // required and must be unique in the user table 
     'password'   => 'required|min:8|alpha_num', 
     'password_confirm' => 'required|same:password',   // required and has to match the password field 
     'mobile'   => 'required', 
     'role_id'   => 'required' 
     ]; 
    } 
} 

回答

10

Laravel在会话数据等的错误会自动检查,一个$errors变量是实际上始终可用于您的所有观点。如果您想在出现任何错误时显示模态,可以尝试如下所示:

<script type="text/javascript"> 
@if (count($errors) > 0) 
    $('#register').modal('show'); 
@endif 
</script> 
+0

非常感谢,并感谢有关$ errors部分的其他知识。 =) – EunJi

相关问题