doubeng3216 2016-11-19 09:28
浏览 37
已采纳

我已经在codeigniter中开发项目但在else条件下形成验证错误没有显示任何错误

Here is My Controller Code:

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'));
    }
}

Here is My view page:

<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>

I had load form_helper and form validation library in constructor. When check_pincode($pincode) method return false that time set_message not working...
what will change into set_message show error.

  • 写回答

1条回答 默认 最新

  • dourao3960 2016-11-19 13:50
    关注

    You are setting the message after running form validation. When you run form validation that is when the set messages are used. If the form validation is passed then the set messages are never used, as they are only used on field failure.

    You could create your own validation test using a callback, making the postcode service area check part of the form validation process. This is a powerful process that is worth getting used to as your forms can literally do anything once you get the hang of it. You can read about it here in the docs.

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

    Alternatively, and a bit more clunky, since you have already run the form validation, and checked if the post code is in service, and found it not to be, you will need to set a new page variable that you send to the view about if postcode_out_of_service is TRUE or FALSE.

    On your form, you would need to use that variable test to output a user message about the postcode problem, it not being serviced.

    What I would do here, is to redirect to a new page, that describes what the user can do if they are not in a serviceable postcode. You could even sell this space to another company so that you could say "Even though we cannot service that area this website can..".

    From a users perspective, the very first stage of registration should be the postcode check, otherwise you have wasted their time entirely. So have stage 1 as 'Check your postcode' or similar.

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真