当我想在fuelphp框架中验证我的表单时遇到问题
这是我在控制器中的代码
/*
* for getting request param for client and save to database
*/
public function action_input(){
$data = array();
//checking method from client
if(Input::method() == 'POST'){
$val = Validation::forge();
$val->add('name','Name')
->add_rule('required');
$val->add('age','Age')
->add_rule('required');
$val->add('alamat','Alamat')
->add_rule('required');
$val->add('email', 'Email address')->add_rule('match_value', 'msofyancs@gmail.com', true)->add_rule('valid_email');
if($val->run()){
$data['name'] = Input::post('name');
$data['body'] = Input::post('age');
$data['alamat'] = Input::post('alamat');
$data['email'] = Input::post('email');
}else{
$data['error'] = $val->errors('name')->get_message('The field :label must be filled out before auth is attempted.');
}
return View::forge('testing/result', $data);
}
}
如果我输入的验证是真的(所有字段都是正确的)那没问题,但是当任何字段不正确时我会出现这样的错误
ErrorException [ Error ]: Call to undefined method Fuel\Core\Validation::errors()
和指向此代码的调试器
$data['error'] = $val->errors('name')->get_message('The field :label must be filled out before auth is attempted.');
我不知道发生了什么,但我仍然在语句顶部声明 $val 但错误未定义,有人知道吗?
我是fuelPHP框架的新手,也许你可以给我建议如何在fuelphp框架中验证表单,比如更好......谢谢你的回答。