codeigniter中的form_validation给出"必需"错误

时间:2016-08-30 01:32:27

标签: php codeigniter

我目前正在开发一个codeigniter项目,我正在尝试使用form_validation库并且我遇到了一个小问题..

我创建了一个验证某些字段的函数。 离。

public function validate_employee_data($data) {
    var_dump("EMPLOYEE");
    $this->form_validation->set_rules($data['firstname'], 'First Name', 'required');
    $this->form_validation->set_rules($data['lastname'], 'Last Name', 'required');
    $this->form_validation->set_rules($data['nickname'], 'Nick Name', 'trequired');
    $this->form_validation->set_rules($data['email'], 'Email Address', 'required|valid_email');
    $this->form_validation->set_rules($data['contact_no'], 'Contact Number', 'required|min_length[11]|max_length[12]');
    $this->form_validation->set_rules($data['group_tags'], 'Group Tags', 'required');

    if($this->form_validation->run() == FALSE) {
        echo validation_errors();
    }
    else {
        // Do something
    }
}

我试过var_dump $ data并且它不是空的但是验证仍然会抛出"(这)字段是必需的",我有点困惑的是这个错误保持的原因是什么出现。

先谢谢你们..

3 个答案:

答案 0 :(得分:1)

答案:表单验证只能与表单字段名称一起使用,而不能与变量本身一起使用。

答案 1 :(得分:1)

昵称字段,trequired?这不是必需的吗?

$this->form_validation->set_rules($data['nickname'], 'Nick Name', 'trequired');

$this->form_validation->set_rules($data['nickname'], 'Nick Name', 'required');

或者您的表单字段可以与验证相匹配,也可以验证

答案 2 :(得分:1)

first of all , please check form validation libraries to load in autoload or your function uppar side, if yes so please ignore it.

Please Use this code

public function validate_employee_data($data) {
    var_dump("EMPLOYEE");
    $this->form_validation->set_rules('firstname', 'First Name', 'required');
    $this->form_validation->set_rules('lastname', 'Last Name', 'required');
    $this->form_validation->set_rules('nickname', 'Nick Name', 'required');
    $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
    $this->form_validation->set_rules('contact_no', 'Contact Number', 'required|min_length[11]|max_length[12]');
    $this->form_validation->set_rules('group_tags', 'Group Tags', 'required');

    if($this->form_validation->run() == FALSE) {
        echo validation_errors();
    }
    else {
        // Do something
    }
}

Ref. for this answer : https://www.codeigniter.com/userguide3/libraries/form_validation.html