Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form_validation errors into array

Tags:

codeigniter

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?

like image 525
Kir Avatar asked Jan 13 '11 04:01

Kir


People also ask

How do you validate CI?

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();

Which rules returns false if the form element is empty?

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.

How many parameters are Set_rules function?

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.


4 Answers

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

like image 161
masu.mo Avatar answered Oct 19 '22 14:10

masu.mo


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);
   }
}
like image 23
Teej Avatar answered Oct 19 '22 14:10

Teej


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();
like image 19
Bretticus Avatar answered Oct 19 '22 14:10

Bretticus


Using the latest version of codeigniter:

print_r($this->form_validation->error_array());

returns:

array("field"=>"message","field2"=>"message2");
like image 12
Bhale Dino Avatar answered Oct 19 '22 15:10

Bhale Dino