Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending form validation in Codeigniter

I have placed this class file called 'My_Form_validation.php' into 'application/core' and I have also tried placing it in 'application/libraries'.

In my controller I am using

$this->form_validation->set_rules('user_postcode', 'Postcode', 'valid_postcode|trim|required|xss_clean');

This is whats in My_Form_validation.php - the actual logic is not in question here because I have a couple of options to actually validate the postcode. What I need help with is understanding why it is not loading or getting called!

My CI version is define('CI_VERSION', '2.0.2');

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Form validation for UK Postcodes
 * 
 * Check that its a valid postcode
 * @author James Mills <[email protected]>
 * @version 1.0
 * @package FriendsSavingMoney
 */

class MY_Form_validation extends CI_Form_validation
{

    function __construct()
    {
        parent::__construct();  
        log_message('debug', '*** Hello from MY_Form_validation ***');
    }

    function valid_postcode($postcode)
    {

        /**
         *
         * UK Postcode validation expression from Wikipedia
         * http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
         *
         * Note: Remember to strtoupper() your postcode before inserting into database!
         *
         */

        $pattern = "/^(GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9]) [0-9][ABD-HJLNP-UW-Z]{2})$/";


        if (preg_match($pattern, strtoupper($postcode)))
    {
            return TRUE;
        } 
        else
        {
            $this->set_message('valid_postcode', 'Please enter a valid postcode');
            return FALSE;
        }
    }
}
like image 758
James Mills Avatar asked Feb 06 '12 13:02

James Mills


People also ask

How to give form validation in CodeIgniter?

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

How to change form validation Error message in CodeIgniter?

PHP_EOL; $data['err'] = $err; $this->load->view('viewname', $data); } else if ($this->form_validation->run() == true ) { #code... } else.. after setting your custom message to $err variable, print it on your view. Save this answer.

How do you validate a form?

Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.


2 Answers

Because you're extending a CodeIgniter library and not a core component, you want to place that in application/libraries (not application/core).

And of course, don't forget to load the Form_validation library within your controller code.

$this->load->library('form_validation');

Other things to check:

  • Filename case sensitivity (MY_Form_validation.php loads while My_Form_validation.php won't)
  • Class name case sensitivity (class MY_Form_validation extends CI_Form_validation)

Reference material:

  • Extending Core Classes
  • Extending Native Libraries
like image 111
CodeIgniterTut Avatar answered Sep 20 '22 06:09

CodeIgniterTut


You have to add $rules on your __construct method and also pass this to parent constructor

eg:

function __construct($rules = array())
{
    parent::__construct($rules);
}

Look at Form_validation and provide same variables.

like image 32
ialbescu Avatar answered Sep 19 '22 06:09

ialbescu