我在 codeigniter 中开发了项目,但在 else conditon 中的表单验证错误没有显示任何错误

I have develop project in codeigniter but form validation error in else conditon not show any error

这是我的控制器代码:

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

    $this->form_validation->set_rules('postcode', 'Postcode',
        'required|trim|integer|max_length[4]',
        array('integer' => 'We are sorry, currently we do not provide services in your area.',
              'max_length[4]'=>'We are sorry, currently we do not provide services in your area.'));

    $this->form_validation->set_error_delimiters("<div class='text-danger'>","</div>");
    if($this->form_validation->run())
    {
        $postcode= $this->input->post('postcode');
        $this->load->model('Common_m');
        $check_code=$this->Common_m->check_pincode($postcode);
        if($check_code)
        {
            //Credentials valid login user
            $this->session->set_userdata('postcode',$check_code);
            $this->load->view('template',array('page_title'=>'pageName','middle_view'=>'schedule_pickup'));
        }
        else
        {
            //authentication failed
            $this->load->view('template',array('page_title'=>'pageName','middle_view'=>'online_order'));
            $this->form_validation->set_message('postcode', 'Service for this postcode is not available.');
        }
    }
    else
    {
        $this->load->view('template',array('page_title'=>'pageName','middle_view'=>'online_order'));
    }
}

这是我的浏览页面:

<div class="container" style="margin:20px;">
    <div class=" col-sm-12 row">
        <div class="col-sm-12">
            <div class="col-sm-12 thumbnail text-center">
                <img alt="" class="img-responsive" src=`"<?php echo base_url(); ?>`images/imageName.jpg">
                <div class="caption">
                    <?php echo form_open('COrders/onlineordercheck', [ 'class' => 'form-horizontal' ]); ?>
                    <div class="title">Postcode</div>

                    <?php echo form_input([ 'name' => 'postcode', 'placeholder' => 'Postcode' ]) ?>
                    <?php echo form_error('postcode'); ?>

                    <?php echo form_submit
                    ([
                        'class' => 'btn btn-primary',
                        'type'  => 'submit',
                        'value' => 'Check Times'
                    ]) ?><?php echo form_close(); ?>
                </div>
            </div>
        </div>
    </div>
</div>

我在构造函数中加载了 form_helper 和表单验证库。当 check_pincode($pincode) 方法 return false 那个时候 set_message 不工作...
什么会变成 set_message 显示错误。

您正在 运行ning 表单验证后设置消息。当您 运行 表单验证时即使用设置的消息。如果表单验证通过,则永远不会使用设置的消息,因为它们仅在字段失败时使用。

您可以使用回调创建自己的验证测试,使 post代码服务区检查成为表单验证过程的一部分。这是一个值得习惯的强大过程,因为一旦掌握了它,您的表单几乎可以做任何事情。您可以在此处的文档中阅读相关内容。

http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

或者,有点笨拙,因为您已经 运行 表单验证,并检查了 post 代码是否在服务中,发现它不在,您将需要设置一个新的页面变量,您发送到关于 postcode_out_of_service 是真还是假的视图。

在您的表单上,您需要使用该变量 test 来输出有关 post代码问题的用户消息,它没有得到服务。

我在这里要做的是重定向到一个新页面,该页面描述了如果用户不在可用的 post 代码中可以做什么。你甚至可以把这个 space 卖给另一家公司,这样你就可以说 "Even though we cannot service that area this website can.."。

从用户的角度来看,注册的第一步应该是 post代码检查,否则您就完全浪费了他们的时间。所以第 1 阶段为 'Check your postcode' 或类似的。

希望对您有所帮助。