Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter `validation_errors()` is not working

I am using version CodeIgniter 2.1.4. I have problem on displaying form validation error. form validation is returning false but validation_errors() is not displaying any errors. I have tried to echo in controller and in view but no result. I am giving request through ajax.no php errors are thrown.

Controller:

<?php
class Dashboard extends Admin_Controller {

   public function ajax_new_dist_center($id=NULL)
   {
       $this->load->model('dist_centre_m');
       $this->load->helper(array('form', 'url'));
       $this->load->library('Form_validation');

       $validation = $this->dist_centre_m->rules;
       $this->form_validation->set_error_delimiters('<li>', '</li>');
       $this->form_validation->set_rules($validation);
       if ($this->form_validation->run() == TRUE)
       {
            if($this->dist_centre_m->create($id))
                echo 'New centre created';
            else
            {
                $this->output->set_status_header('404');
                echo 'Given center not found';
            }
       }
       else
       {
           $this->output->set_status_header('400');
           echo 'validation Failed';
           $this->load->view('alert_error');
       }
   }
}

View:

<?php echo validation_errors();?>
<p>Testing Error</p>

Model:

class dist_centre_m extends MY_Model {
    protected $_table_name = 'distribution_centre';
    protected $_primary_key = 'dis_id';
    protected $_order_by = 'dis_id';
    public $rules = array(
        'name' => array( 
            'field'=>'name',
            'label'=>'Center name',
            'rules'=>'trim|required|xss_cleaned|min_length[3]|max_length[45]'
        ),
        'street' => array(
            'field'=>'street',
            'label'=>'Street',
            'rules'=>'trim|required|xss_cleaned|min_length[3]|max_length[45]'
        ),
        'town' => array(
            'field'=>'town',
            'label'=>'Town',
            'rules'=>'trim|required|min_length[3]|max_length[45]|required|xss_cleaned'
        ),
        'postcode' => array(
            'field'=>'postcode',
            'label'=>'Postcode',
            'rules'=>'trim|required|max_length[10]|required|xss_cleaned'
        ),
        'tel' => array(
            'field'=>'tel',
            'label'=>'Telephone number',
            'rules'=>'trim|valid|exact_length[11]|required|xss_cleaned'
        ),
    );

    public function __construct() {
        parent::__construct();
        $this->load->helper('security'); 
    }

    public function create($id=NULL){
        $data = array(
                'name'      =>$this->input->post('name',true),
                'street'    =>$this->input->post('street',true),
                'town'      =>$this->input->post('town',true),
                'postcode'  =>$this->input->post('postcode',true),
                'tel'       =>$this->input->post('tel',true),
        );
        return $this->save($data,$id);
    }  
} 

Output:

validation Failed

Testing Error

None of the related question is working please do not mark duplicate.

Update: My Ajax request page:

 <form id="new-center-form" method="post">
                <div class="row">
                    <div class="col-md-8">
                        <div class="form-group">
                            <label for="name">Name</label>
                            <input type="text" name="name" placeholder="Name"/>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for='street'>Street</label>
                            <input type="text" name="street" placeholder="Street"/>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="town">Town</label>
                            <input type="text" name="town" placeholder="Town"/>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="postcode">Postcode</label>
                            <input type="text" name="postcode" placeholder="Postcode"/>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="tel">Phone</label>
                            <input type="text" name="tel" placeholder="Telephone"/>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-8">
                        <div class="form-group">
                            <button id="centre-submit" class="btn btn-primary pull-left">Save</button>
                        </div>
                    </div>
                </div>
            </form>


<script type="text/javascript">
    $(document).ready(function(){

        var url_new_center = '<?php echo page_url('new-center') ?>';

        $('#centre-submit').click(function(e){
            e.preventDefault();
            $.ajax({
                url:url_new_center,
                type:'post',
                data: $('new-center-form').serialize()
            }).done(function(response){
                $('#infos').html(response);
                $('#infos').slideDown();
                $('.new-centre').slideUp();
            }).fail(function(response){
                $('#errors').html(response);
                $('#errors').slideDown();
            });
        })
    });
</script>
like image 985
Sanoob Avatar asked Mar 11 '14 21:03

Sanoob


2 Answers

Finally after a long sleep I figured out.$this->form_validation->run() returns false when no $_POST data found and it will not set any errors to print. In my ajax request due to some errors it could not send any data. Thanks for curl I was able to test it.

update

 data: $('new-center-form').serialize() // this was the error. 

I made a typo here. this should be like this data: $('#new-center-form').serialize()

like image 156
Sanoob Avatar answered Oct 15 '22 09:10

Sanoob


let me make your answer more clear.

It is not because no $_POST data found like you said, it is actually because that there are no RELATED $_POST data that you have set rules for them in your model.

There may have been any other $_POST data exclude for those that you have set rules for:

'name' , 'street' , 'town' , 'postcode' , ' tel'

then the CI validator could not find any of the elements that rules have been set so the form_validation->run() will definitely return false

And certainly the validation_errors() will not show anything because there are nothing to apply the specified rules on.

like image 22
Lê Gia Lễ Avatar answered Oct 15 '22 09:10

Lê Gia Lễ