CI3 /验证在初始加载时始终返回false

时间:2018-10-02 01:00:29

标签: php codeigniter

我不明白为什么加载时验证总是返回false。这是我的控制器的一部分:

 // load up the validation rules for blog Info form
    $this->config->load('mh_blog_validate');
    $this->form_validation->set_rules($this->config->item('validate_blog_update'));                                                          
    if ($this->form_validation->run('validate_blog_update') === FALSE) {
        $errors     = array('message'   => $this->upload->display_errors());
                    $message    = array('message'   => 'Warning - '.$errors['message'],
                                        'class'     => 'danger',  
                                        );
                     $this->data['alert']   = bootstrap_alert($message);          
    }

这是我来自mh_blog_validate的验证配置:

 $config['validate_blog_update'] =  array(
        'title' => array(
            'field' => 'title',
            'label' => '',
            'rules' => 'required|trim|xss_clean|min_length[5]|callback_is_slug_unique_on_update[]',

            'errors' => array(
                'required'      => 'The title cannot be blank.',
                'min_length'    => 'The title must be 5 charaters or more.',
                'is_unique'     => 'The title must be unique.',
                'is_slug_unique_on_update' => 'The new title needs to be unique'
            ),
        ),

        'body' => array(
            'field' => 'body',
            'label' => '',
            'rules' => 'required|trim|xss_clean|min_length[5]',
            'errors' => array(
                'required'    => 'The body cannot be blank',
                'min_length' => 'The body must be 5 charaters or more.',
            )    

        ),
); // end validate_blog_create

这是我在验证中使用的回调函数:

function is_slug_unique_on_update() {      
    $new_slug    = url_title($this->input->post('title'));
    if ( $new_slug == $this->input->post('slug')) {
        // no change in slug so update
        // echo "no change in title";
        return TRUE;
    } elseif ( $new_slug !== $this->input->post('slug')) {
        // new slug
        $result = $this->Blog_model->is_slug_unique_on_update($new_slug);
        return $result; // returns FALSE if the title is not unique
    }
}

我在视图中收到的输出是“警告-”,并将其放置在视图中:

    if (isset($this->data['alert']){
                        echo $this->data['alert'];
                       }

我期望验证不会产生错误,因为我尚未提交表单。即使我还没有提交我认为的表格,它也可能运行验证(?)。

+++新编辑+++

在下面添加了有效的代码,并希望知道为什么我的代码不起作用。我以为我的代码遵循相同的模式,不是吗?      类Form扩展了CI_Controller {

    public function index()
    {
            $this->load->helper(array('form', 'url'));

            $this->load->library('form_validation');

            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required',
                    array('required' => 'You must provide a %s.')
            );
            $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required');

            if ($this->form_validation->run() == FALSE)
            {
                    $this->load->view('myform');
            }
            else
            {
                    $this->load->view('formsuccess');
            }
    }
 }

1 个答案:

答案 0 :(得分:1)

问题在于,无论表单是否提交数据,您都在设置$this->data['alert']值。当然,您可以通过添加条件来阻止此变量分配,因此只有在提交任何$_POST数据时才进行设置:

// load up the validation rules for blog Info form
$this->config->load('mh_blog_validate');
$this->form_validation->set_rules($this->config->item('validate_blog_update'));                                                          
if ($this->form_validation->run('validate_blog_update') === FALSE) {
    if ($_POST)
    {
        $errors     = array('message'   => $this->upload->display_errors());
                    $message    = array('message'   => 'Warning - '.$errors['message'],
                                        'class'     => 'danger',  
                                        );
                    $this->data['alert']   = bootstrap_alert($message);          
    }
}