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

我已经在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.

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.