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>
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With