尽管 form_validation 很重要,但我发现自己有时更喜欢其他解决方案。
添加这个:
库/My_Form_validation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
/* Custom function to create errors manually
* */
function create_error($str_field, $str_message) {
$str_message = t($str_message, $str_line);
$this->_field_data[$str_field] =
array('error' => $str_message,
'is_array' => FALSE,
'postdata' => $this->CI->input->post($str_field),
'rules' => '',
'field' => $str_field);
$this->_error_array[$str_field] = $str_message;
}
}
然后你可以在你的控制器中手动运行它->run()
。
尽可能获取查询,并为每种情况设置一个 if,如下所示:
$arr_uniques = array('email', 'name', 'snn'); // whatev
foreach($arr_uniques as $h)
if ($row->$h == $this->input->post($h))
$this->form_validation->create_error($h, "{$h} is not unique!");