Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom codeigniter validation rule

Tags:

I have a function in my login form that checks if the email and password match the values in the database and if so it logs the user into the system.

I would like to display a validation error if this function returns false.

My problem is that I am unsure how to go about creating this. The message relates to both the password and email fields so I would not want a rule for each input field just display a single message.

I have tried using flashdata to achieve this but it only works when the page has been refreshed.

How can I created a new validation rule solely for the function $this->members_model->validate_member() ??

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');         $this->form_validation->set_rules('email_address', '"Email address"', 'trim|required|valid_email');         $this->form_validation->set_rules('password', '"Password"', 'trim|required');          if ($this->form_validation->run() == FALSE)         {             $viewdata['main_content'] = 'members/login';             $this->load->view('includes/template', $viewdata);         }         else         {                        if($this->members_model->validate_member())                 { 
like image 824
hairynuggets Avatar asked Nov 18 '11 11:11

hairynuggets


People also ask

How to set validation rules 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 give form validation in CodeIgniter?

Create a view file myform. php and save the below code it in application/views/myform. php. This page will display form where user can submit his name and we will validate this page to ensure that it should not be empty while submitting.

How to set custom error message in CodeIgniter form validation?

Show activity on this post. if(YOUR_CONDITION){ $this->form_validation->run(); $err = validation_errors(); $err = $err. '<p>Custom validation error message</p>'. PHP_EOL; $data['err'] = $err; $this->load->view('viewname', $data); } else if ($this->form_validation->run() == true ) { #code... } else..


2 Answers

You use the callback_ in your rules, see callbacks, for ex.

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

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

function validate_member($str) {    $field_value = $str; //this is redundant, but it's to show you how    //the content of the fields gets automatically passed to the method     if($this->members_model->validate_member($field_value))    {      return TRUE;    }    else    {      return FALSE;    } } 

You then need to create a corresponding error in case the validation fails

$this->form_validation->set_message('validate_member','Member is not valid!'); 
like image 167
Damien Pirsy Avatar answered Sep 16 '22 19:09

Damien Pirsy


One best way to achieve this is extending CodeIgniter’s Form Validation library. Let say we want to create a custom validator named access_code_unique for the field access_code of the database table users.

All you have to do is creating a Class file named MY_Form_validation.php in application/libraries directory. The method should always return TRUE OR FALSE

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');  class MY_Form_validation extends CI_Form_validation {     protected $CI;      public function __construct() {         parent::__construct();             // reference to the CodeIgniter super object         $this->CI =& get_instance();     }      public function access_code_unique($access_code, $table_name) {         $this->CI->form_validation->set_message('access_code_unique', $this->CI->lang->line('access_code_invalid'));          $where = array (             'access_code' => $access_code         );          $query = $this->CI->db->limit(1)->get_where($table_name, $where);         return $query->num_rows() === 0;     } } 

Now you can easily add your new created rule

$this->form_validation->set_rules('access_code', $this->lang->line('access_code'), 'trim|xss_clean|access_code_unique[users]');  
like image 26
BoCyrill Avatar answered Sep 17 '22 19:09

BoCyrill