How to use Custom validation in codeigniter?

Custom validation in codeigniter framework

we can use custom validation in two ways.

  • By Using Callbacks.
  • By Extend CodeIgniter’s Form Validation library.
  • Custom validation Rules Using Callbacks

    define your custom rule with callback_ prefix ,

    $this->form_validation->set_rules('email_address', '"Email address"', 'trim|callback_email_check');

    Then add the method in the controller. This method needs to return either TRUE or FALSE

    function email_check($value)
    {
    if($value) { // do your validations
    return TRUE;
    } else {
    return FALSE;
    }
    }

    The Final Step ,create a corresponding error Message to show on validation fail

    $this->form_validation->set_message('email_check','Email is not valid');


    Custom validation Rules By Extend CodeIgniter’s Form Validation library

    Create a new PHP’s Class file named MY_Form_validation.php and put it in the application/libraries/ directory, and extend it with CodeIgniter’s validation library.

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

    class MY_Form_validation extends CI_Form_validation {
    protected $CI;
    function __construct() {
    parent::__construct();
    // reference to the CodeIgniter super object
    $this->CI =& get_instance();
    }
    function email_check($str) {
    $this->CI->form_validation->set_message('email_check', 'The %s is not valid.');
    if($value) { // do your validations
    return TRUE;
    } else {
    return FALSE;
    }
    }
    }

    now add this to your validation rules

    $this->form_validation->set_rules('email_address', '"Email address"', 'trim|email_check');

    Example : callback

    $this->form_validation->set_rules('year', 'Year', 'trim|required|xss_clean|callback_year_validation');

    function year_validation($str) {
    // $str will be field value which post. will get auto and pass to function.
    $year = date('Y');
    if ($str <= $year) {
    $this->form_validation->set_message("year_validation", 'year should be greater then current year.');
    return FALSE;
    }
    else {
    return TRUE;
    }
    }

    How to validate only alpha character with space in codeigniter?

    ^ and $ Tells that it is the beginning and the end of the string
    a-z are lowercase letters, A-Z are uppercase letters
    \s is whitespace and + means 1 or more times.

    $this->form_validation->set_rules('full_name', 'Full Name', 'min_length[7]|trim|required|callback_alpha_space');

    function alpha_space($fullname){
    if (! preg_match('/^[a-zA-Z\s]+$/', $fullname)) {
    $this->form_validation->set_message('alpha_space', 'The %s field contain only alpha characters & White spaces');
    return FALSE;
    } else {
    return TRUE;
    }
    }

    How to validate only alpha-numeric, dashes, spaces in codeigniter?

    ^ and $ Tells that it is the beginning and the end of the string
    a-z are lowercase letters, A-Z are uppercase letters
    \s is whitespace and + means 1 or more times.

    $this->form_validation->set_rules('full_name', 'Full Name', 'trim|required|callback_alpha_space');

    public function alpha_numeric_dash_space($str='')
    {
    if (! preg_match("/^([-a-z0-9_ ])+$/i", $str))
    {
    $this->form_validation->set_message('alpha_numeric_dash_space', 'The %s field only contain alpha-numeric,underscores & dashes.');
    return FALSE;
    }
    else
    {
    return TRUE;
    }
    }

    Alpha characters and space.

    $this->form_validation->set_rules('field', 'Field', 'regex_match[/^([a-z ])+$/i]');

    $this->form_validation->set_rules('name', 'Name', trim|required|callback_alpha_dash_space');


    function alpha_dash_space($str)
    {
    return ( ! preg_match("/^([-a-z_ ])+$/i", $str)) ? FALSE : TRUE;
    }

    array(
    'field'=>'ptitle',
    'label'=>'Property Title',
    'rules'=>'trim|required|xss_clean|regex_match[/^[][a-zA-Z0-9@# ,().]+$/]'
    )

    File upload validation

    if(empty($_FILES['fileupload']['name']))
    {
    $this->form_validation->set_rules('fileupload', 'My Upload', 'trim|required');
    }
    if(empty($_FILES['fileupload']['name']))
    {
    $this->form_validation->set_rules('fileupload', 'File Upload', 'trim|required|callback_file_check_fileupload');
    }


    public function file_check_fileupload($str){
    $allowed_mime_type_arr = array('application/pdf');
    $mime = get_mime_by_extension($_FILES['fileupload']['name']);
    if(isset($_FILES['fileupload']['name']) && $_FILES['fileupload']['name']!=""){
    if(in_array($mime, $allowed_mime_type_arr)){
    return true;
    }else{
    $this->form_validation->set_message('file_check_fileupload', 'Please select only pdf file.');
    return false;
    }
    }else{
    $this->form_validation->set_message('file_check_fileupload', 'Please Select fileupload to upload.');
    return false;
    }
    }

    $this->form_validation->set_rules("photograph", "Photograph", "callback_file_check_photograph");


    Call back function of image upload validation.

    public function file_check_photograph($str){
    $allowed_mime_type_arr = array('image/jpeg','image/jpg','image/png');
    $mime = get_mime_by_extension($_FILES['photograph']['name']);
    if(isset($_FILES['photograph']['name']) && $_FILES['photograph']['name']!=""){
    if(in_array($mime, $allowed_mime_type_arr)){
    return true;
    }else{
    $this->form_validation->set_message('file_check_photograph', 'Please select only jpg/jpeg/png image.');
    return false;
    }
    }else{
    $this->form_validation->set_message('file_check_photograph', 'Please choose Photograph to upload.');
    return false;
    }
    }





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here