Custom form validation Codeigniter

Some validation function are already defined in codeigniter form validation library. But sometimes, we might need to have a custom validation. You can do it by –

There are 2 ways to implement custom form validation in codeigniter

1.) Create function in controller (in the same controller in which we are implementing the validation).
2.) Create own or custom library and create function in that file.

1.) Create function in same controller –

Create function and call it by using callback_.

Example – Here is an example how to validate Indian mobile number using custom form validation function –

public function _check_phone($phone)

{

  if(preg_match(‘/^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/’,$phone))

{

return true;

} else {

$this->form_validation->set_message(‘_check_phone’, ‘%s ‘.$phone.’ is invalid format’);

return false;

}

}

Function Call –

$this->form_validation->set_rules(‘contact_no_1’, ‘Mobile Number’, ‘trim|required|xss_clean|min_length[10]|max_length[18]|callback__check_phone’);

Disadvantage – You can use that function only that controller. What will you do if you have to put the validation at multiple places (controllers) ? So, its better to create custom library and create function in that library.

2.) Create Custom Library –

Create custom library file(say my_form_validation) in application/libraries directory.

Example – Same Indian mobile number validation –

<?php 

/**

 * CIMembership

 * 

 * @package Libraries

 * @author 1stcoder Team

 * @copyright   Copyright (c) 2015 1stcoder. (http://www.1stcoder.com)

 * @license http://opensource.org/licenses/MIT MIT License

 */

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

class MY_Form_validation extends CI_Form_validation 

{

protected $ci;

function __construct() {

parent::__construct();

$this->ci =& get_instance();

}

public function _check_phone($phone)

{

$this->ci->form_validation->set_message(‘_check_phone’, “This %s “.$phone.” is in invalid format!”);

  if(preg_match(‘/^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/’,$phone))

{

return true;

} else {

return false;

}

}

}

Function Call –

 $this->load->library(‘my_form_validation’); //load the library

$this->form_validation->set_rules(‘contact_no_1’, ‘Mobile Number’, ‘trim|required|xss_clean|min_length[10]|max_length[18]|_check_phone’); // Call the functions like others

Note: You can create the function in both – controller and custom library But always prefer to write the function by creating custom library, so that in case, if need to use that function in multiple controllers, can be used easily.

 
 
 
____________________________________________
For more related queries, please visit.
In case, if you did not find your solution or have any query/question, please contact us.

Codeigniter Interview Questions

I am sharing my personal exp. of interview. Prepare well before appearing in interview. Following is the list of questions that can be asked during the interview ..

Ques: How will you connect multiple databases in Codeigniter ?

Ans. In Application/Config/database.php –

Copy default database connection array and paste it and change database connection details and change the $db[‘default’] to $db[‘new_db’] in pasted array.

In model – 

public function __construct(){

parent::__construct();

$this->cipaypal_db = $this->load->database(‘new_db’,TRUE); 

/* $this->cipaypal_db = Variable Name and in $this->load->database(name) = should be same as $db[name] in database.php file */

}

Ques: What are logs in codeigniter ? What is the meaning of log status(0,1,2,3,4) ?

Ans. Logs are used for error handling. You can write your own message in it.

0 = Disables logging, Error logging TURNED OFF

1 = Error Messages (including PHP errors)

2 = Debug Messages

3 = Informational Messages

4 = All Messages

Ques: What are hooks ? Name all hooks.

Ans. Hooks are used to override the default functionality of codeigniter.

To enable hooks – 

$config[‘enable_hooks’] = TRUE;  /Application/config.php

There are 7 types of hooks available in codeigniter –

1.) pre_system

2.) pre_controller

3.) post_controller_constructor

4.) post_controller

5.) display_override

6.) cache_override

7.) post_system

Ques: Which protocol is used in Routing ?

Ans: Transmission Control Protocol (TCP).

Ques: How will you make database connection in routes in Codeigniter ? How will you define routing based on database value ?

Ans: require_once( BASEPATH .’database/DB.php’ );   //Include file

$db =& DB(); //Create instance of database

$query = $db->get( ‘table’ ); //Table for routing base on which routing will be done. 

$result = $query->result();

Ques: What are in inhibitors ?

Ans: It is a class that handles the error. It uses PHP native error handling functions like set_error_handler, get_exception_handler, register_shutdown_function to handle errors.

Ques: Where routing is defined in Codeigniter ?

Ans: In application/configuration/routes.php

 

 

For more related queries, please visit.
In case, if you did not find your solution or have any query/question, please contact us.