我正在尝试为我的表单进行后端验证,但它不起作用。它总是表明即使我提交了值,验证也会失败。
$validation = Validator::make(Input::all(), array(
array('email' => 'required'),
array('password' => 'required')
));
if ($validation->fails()) { // This is always failing
echo '<pre>';
print_r(Input::all());
die();
}
die('everything is OK!');
Array
(
[_token] => ZnzZ2aDoTABIZZkvwxZoa7IjHkvK25ndibis5AbA
[email] => somedata
[password] => somemoredata
)
正如你所看到的那样,即使从Input:all()
清楚地表明价值已经设定,它也失败了。
答案 0 :(得分:6)
试试这个:
$validation = Validator::make(Input::all(), array(
'email' => 'required',
'password' => 'required'
));
不是你做的双数组:
$validation = Validator::make(Input::all(), array(
array('email' => 'required'), // Is shouldn't be an array here.
array('password' => 'required')
));
答案 1 :(得分:4)
变化:
$validation = Validator::make(Input::all(), array(
array('email' => 'required'),
array('password' => 'required')
));
到
$validation = Validator::make(Input::all(), array(
'email' => 'required|email', // Note that I added in valid email rule here also
'password' => 'required'
));