Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms codeigniter表单验证始终返回false,即使满足规则_Forms_Codeigniter_Validation_Submit - Fatal编程技术网

Forms codeigniter表单验证始终返回false,即使满足规则

Forms codeigniter表单验证始终返回false,即使满足规则,forms,codeigniter,validation,submit,Forms,Codeigniter,Validation,Submit,问题:即使我的表单字段符合规则,codeigniter表单验证仍然返回false(错误) 例如: public function edit(){ $this->load->library('form_validation'); $this->form_validation->set_rules('title', 'title', 'required|xss_clean|trim|is_unique[news.Title]'); $this-

问题:即使我的表单字段符合规则,codeigniter表单验证仍然返回false(错误)

例如:

   public function edit(){
    $this->load->library('form_validation');

    $this->form_validation->set_rules('title', 'title', 'required|xss_clean|trim|is_unique[news.Title]');
    $this->form_validation->set_rules('description', 'description', 'required|xss_clean|trim');
    $this->form_validation->set_rules('notes', 'notes', 'required|xss_clean|trim');

    if ($this->form_validation->run() == FALSE)
    {
        $data['error'] =  '';
        $data['page_title']="Edit News";
        echo "error";

    }
    else {
    ..........

如果我将字段留空,它会告诉我输入一些内容,因为它们不能留空。如果我输入了什么,那么一旦我提交表单,它就会返回错误。

您必须使用回调验证函数

你也必须通过身份证

$this->form_validation->set_rules('title', 'Title', 'required|xss_clean|trim|callback_check_title');

function check_title($title) {        
    if($this->input->post('id'))
        $id = $this->input->post('id');
    else
        $id = '';
    $result = $this->news_model->check_unique_title($id, $title);
    if($result == 0)
        $response = true;
    else {
        $this->form_validation->set_message('check_title', 'Title must be unique');
        $response = false;
    }
    return $response;
}
模型中

function check_unique_title($id, $title) {
    $this->db->where('title', $title);
    if($id) {
        $this->db->where_not_in('id', $id);
    }
    return $this->db->get('news')->num_rows();
}

它可以用于插入和更新,无需回调函数

问题是:没有加载codeigniter数据库类; 您应该加载数据库:$this->load->database();跑步前

if ($this->form_validation->run() == FALSE)
{
    $data['error'] =  '';
    $data['page_title']="Edit News";
    echo "error";

}

它区分大小写吗?尝试将
news.Title
更改为
news.Title
。在if than块的第一部分中,如果表单验证失败,您应该加载表单视图。
fields==哪个字段
?您应该打印失败的验证。在您的案例中,唯一会引发错误的是is_unique函数。这就是问题的根源。像其他人建议检查区分大小写一样,确保DB列存在,使用您提供的少量代码很难判断,因为您没有发布适当的代码段,以便我们确认/否认它是否正常工作。此答案与问题无关。谢谢。在加载视图时,我忘记了传递$data,以防出错。哎呀!他犯了完全相同的错误。因为我到处都需要
数据库
,所以我添加了这个
$autoload['libraries']=array('database')config>autoload.php中的code>修复了我的问题