php - CodeIgniter `validation_errors()` is not working -


i using version codeigniter 2.1.4. have problem on displaying form validation error. form validation returning false validation_errors() not displaying errors. have tried echo in controller , in view no result. giving request through ajax.no php errors 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 related question working please not mark duplicate.

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

finally after long sleep figured out.$this->form_validation->run() returns false when no $_post data found , not set errors print. in ajax request due errors not send data. curl able test it.

update

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

i made typo here. should data: $('#new-center-form').serialize()


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -