我正在尝试在我的控制器中的操作中进行验证。一旦失败,我将验证器产生的消息发送到同一控制器中的另一个函数,以将其传递给视图。唯一的问题是我没有看到任何显示在视图中,并且一旦我点击提交并且错误发生,网址就会变为奇怪的格式。
控制器:
<?php
class MembersController extends BaseController {
protected $layout = 'layouts.master';
public function loadRegisterView($input = null)
{
if($input == null)
return View::make('members.register');
else
return View::make('members.register', $input);
}
public function loadLoginView()
{
return View::make('members.login');
}
public function register()
{
$rules = array(
'first_name' => 'Required|Min:3|Max:88|Alpha',
'last_name' => 'Required|Min:3|Max:88|Alpha',
'password' => 'Required|Min:3|Max:88|Alpha|Confirmed',
'email' => 'Required|email|unique:users',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
$messages = $validator->messages();
return Redirect::action('MembersController@loadRegisterView', $messages);
}else{
return "Thank you!";
}
}
}
路线:
Route::get('login', array('as' => 'login', 'uses' => 'MembersController@loadLoginView'));
Route::get('signup', array('as' => 'signup', 'uses' => 'MembersController@loadRegisterView'));
Route::get('/', 'MainController@index');
Route::post('processRegistration', array('as' => 'processRegistration', 'uses' => 'MembersController@register'));
视图中的:
@if ( $errors->count() > 0 )
<p>The following errors have occurred:</p>
<ul>
@foreach( $errors->all() as $message )
<li>{{ $message }}</li>
@endforeach
</ul>
@endif
,网址如下:
http://localhost:8888/store/index.php/signup?%00%2A%00messages%5Bfirst_name%5D%5B0%5D=The+first+name+field+is+required.&%00%2A%00messages%5Blast_name%5D%5B0%5D=The+last+name+field+is+required.&%00%2A%00messages%5Bpassword%5D%5B0%5D=The+password+field+is+required.&%00%2A%00messages%5Bemail%5D%5B0%5D=The+email+field+is+required.&%00%2A%00format=%3Amessage
提前致谢
答案 0 :(得分:6)
您可以将变量直接传递给路线,而您应该这样做:
if ($validator->fails())
{
$messages = $validator->messages();
return Redirect::action('MembersController@loadRegisterView')->withErrors($messages);
}else{
return "Thank you!";
}