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
Php Codeigniter 3表单验证未按预期工作_Php_Codeigniter - Fatal编程技术网

Php Codeigniter 3表单验证未按预期工作

Php Codeigniter 3表单验证未按预期工作,php,codeigniter,Php,Codeigniter,CI3文档明确指出,“由于您还没有告诉表单验证类验证任何内容,因此默认情况下它返回FALSE(boolean FALSE)。run()方法仅在成功应用了您的规则且没有任何失败的情况下返回TRUE。” if ($this->form_validation->run() == FALSE) { $this->load->view($this->mh_template, $this->data); } else {

CI3文档明确指出,“由于您还没有告诉表单验证类验证任何内容,因此默认情况下它返回FALSE(boolean FALSE)。
run()
方法仅在成功应用了您的规则且没有任何失败的情况下返回TRUE。”

    if ($this->form_validation->run() == FALSE) {

        $this->load->view($this->mh_template, $this->data);


    }   else {

        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->session->set_flashdata('message', $this->data['alert'] );              
        redirect('/MH_app_private/mh_app_settings');
    }
我有下面的方法snipplet。显然,在第一个视图中,视图总是会发出“警告-ooops something gours”但没有验证错误()。我该如何着手解决这个问题?请检查一下表格是否是$U POST?关于如何完成的最佳方式或建议是什么?bootstrap_alert()只是将错误或成功消息包装到引导警报中

    if ($this->form_validation->run() == FALSE) {
        $message = array(   'message'   => 'Warning - Ooops something went wrong '.validation_errors(),
                            'class'     => 'danger',  // must be warning, danger, success or info.
                    );
        $this->data['alert'] = bootstrap_alert($message);
        $this->load->view($this->mh_template, $this->data);
    }   else {
        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->load->view($this->mh_template, $this->data); 
    }
    if ($this->form_validation->run() == FALSE) {

        $this->load->view($this->mh_template, $this->data);


    }   else {

        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->session->set_flashdata('message', $this->data['alert'] );              
        redirect('/MH_app_private/mh_app_settings');
    }
这就是我最终要做的:

    if ($this->form_validation->run() == FALSE) {

        $this->load->view($this->mh_template, $this->data);


    }   else {

        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->session->set_flashdata('message', $this->data['alert'] );              
        redirect('/MH_app_private/mh_app_settings');
    }
//设置表单规则 $this->form_validation->set_规则(“时区”、“时区”、“必需”)

    if ($this->form_validation->run() == FALSE) {

        $this->load->view($this->mh_template, $this->data);


    }   else {

        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->session->set_flashdata('message', $this->data['alert'] );              
        redirect('/MH_app_private/mh_app_settings');
    }

检查请求是否为POST显示或不显示警告。

我没有意识到我可以使用以下内容格式化验证错误()

    if ($this->form_validation->run() == FALSE) {

        $this->load->view($this->mh_template, $this->data);


    }   else {

        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->session->set_flashdata('message', $this->data['alert'] );              
        redirect('/MH_app_private/mh_app_settings');
    }
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');  
$this->form\u validation->set\u error\u分隔符(“”,);

这最终解决了问题

你能发布你的整个函数以便我们检查剩下的东西吗?你加载了form_验证库了吗?在初始视图中,没有错误,但是,显示了消息数组(没有验证错误)。在post上,当我故意出错时,消息数组会显示验证错误。成功也是在适当的时候展现出来的。问题在于最初的观点。是的,form_验证已加载。我很好奇这是如何解决问题的。你能给我看看吗?
    if ($this->form_validation->run() == FALSE) {

        $this->load->view($this->mh_template, $this->data);


    }   else {

        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->session->set_flashdata('message', $this->data['alert'] );              
        redirect('/MH_app_private/mh_app_settings');
    }