我在我的CodeIgniter应用程序中有一个表单验证代码:
$this->load->library('form_validation');
$this->form_validation->set_rules('message', 'Message', 'trim|xss_clean|required');
$this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required');
if($this->form_validation->run() == FALSE)
{
// some errors
}
else
{
// do smth
$response = array(
'message' => "It works!"
);
echo json_encode($response);
}表单是基于AJAX的,因此前端必须接收带有表单错误的JSON数组,例如:
array (
'email' => 'Bad email!',
'password' => '6 symbols only!',
)如何在CodeIgniter?中获得具有表单验证错误的列表或数组
发布于 2013-03-21 14:30:40
我最喜欢的解决方案不涉及在任何地方添加函数或扩展验证库:
$validator =& _get_validation_object();
$error_messages = $validator->_error_array;参考资料:http://thesimplesynthesis.com/post/how-to-get-form-validation-errors-as-an-array-in-codeigniter/
您应该能够在代码中的任何一点上调用它。另外,值得注意的是,有一个前螺纹也讨论了这个问题。
https://stackoverflow.com/questions/4676915
复制相似问题