这是我在控制器中的代码
/*
* 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框架中验证表格更好...感谢您的回答。
答案 0 :(得分:2)
来自manual:
// get an array of validation errors as field => error pairs
$errors = $val->error();