I have a code for form validating in my CodeIgniter app:
$this->load->library('form_validation');
$this->form_validation->set_rules('message', 'Message', 'trim|xss_clean|required');
$this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required');
if($this->form_validation->run() == FALSE)
{
// some errors
}
else
{
// do smth
$response = array(
'message' => "It works!"
);
echo json_encode($response);
}
The form is AJAX-based, so frontend have to receive a JSON array with form errors, for example:
array (
'email' => 'Bad email!',
'password' => '6 symbols only!',
)
How to get such list or array with form validation errors in CodeIgniter?
Setting Validation Rules CodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method: $this->form_validation->set_rules();
Validation Rule Reference. Given below are the most commonly used list of native rules available to use. Returns FALSE if the form element is empty. Returns FALSE if the form element does not match the one in the parameter.
Setting Validation Rules To set validation rules you will use the set_rules() function: $this->form_validation->set_rules(); The above function takes three parameters as input: The field name - the exact name you've given the form field.
<?php
class MY_Form_validation extends CI_Form_validation
{
function __construct($config = array())
{
parent::__construct($config);
}
function error_array()
{
if (count($this->_error_array) === 0)
return FALSE;
else
return $this->_error_array;
}
}
Then you could try the following from your controller:
$errors = $this->form_validation->error_array();
Reference: validation_errors as an array
You just echo validation_errors()
from your controller.
have your javascript place
it in your view.
PHP
// controller code
if ($this->form_validation->run() === TRUE)
{
//save stuff
}
else
{
echo validation_errors();
}
Javascript
// jquery
$.post(<?php site_url('controller/method')?>, function(data) {
$('.errors').html(data);
});
If you really want to use JSON, jquery automatically parses JSON. You can loop through it and append
into your html.
in case you need validation errors as array you can append this function to the form_helper.php
if (!function_exists('validation_errors_array')) {
function validation_errors_array($prefix = '', $suffix = '') {
if (FALSE === ($OBJ = & _get_validation_object())) {
return '';
}
return $OBJ->error_array($prefix, $suffix);
}
}
If you prefer a library method, you can extend the Form_validation class instead.
class MY_Form_validation extends CI_Form_validation {
public function error_array() {
return $this->_error_array;
}
}
and subsequently call it in your controller/method.
$errors = $this->form_validation->error_array();
Using the latest version of codeigniter:
print_r($this->form_validation->error_array());
returns:
array("field"=>"message","field2"=>"message2");
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