I have a code for form validating in my CodeIgniter app:
$this->load->library('form_validation');
$this->form_validatio开发者_如何学JAVAn->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?
application/libraries/MY_Form_validation.php
<?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");
Simply use...
$errors = $this->form_validation->error_array();
I've extended form validation helper:
if ( ! function_exists('validation_errors_array'))
{
function validation_errors_array()
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
return '';
}
// No errrors, validation passes!
if (count($OBJ->_error_array) === 0)
{
return '';
}
// Generate the error string
$array = '';
foreach ($OBJ->_error_array as $key => $val)
{
if ($val != '')
{
$array[$key]= $val;
}
}
return $array;
}
}
from : http://darrenonthe.net/2011/05/10/get-codeigniter-form-validation-errors-as-an-array/
By default, the Codeigniter Form Validation errors are returned as a string:
return validation_errors();;
The solution I like best doesn't involve adding a function anywhere or extending the validation library:
$validator =& _get_validation_object();
$error_messages = $validator->_error_array;
Reference: http://thesimplesynthesis.com/post/how-to-get-form-validation-errors-as-an-array-in-codeigniter/
You should be able to call this at any point in your code. Also, it's worth noting there is a previous thread that discusses this as well.
精彩评论