我的 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 中获取带有表单验证错误的此类列表或数组?
应用程序/库/MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation
{
function __construct($config = array())
{
parent::__construct($config);
}
function error_array()
{
if (count($this->_error_array) === 0)
return FALSE;
else
return $this->_error_array;
}
}
然后您可以从控制器尝试以下操作:
$errors = $this->form_validation->error_array();
参考:validation_errors 作为数组 http://darrenonthe.net/2011/05/10/get-codeigniter-form-validation-errors-as-an-array/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)