26

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

2 Answers 2

51

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!');
3
  • 1
    @Ivan It's not necessary, but can add on readability, thank you Nov 18, 2011 at 12:18
  • 9
    May not be necessary, but a leading underscore will prevent the method from being accessible via "/controller_name/validate_member/blah" ... and using a double-underscore is perfectly acceptable IMO "callback__validate_member" ;)
    – Mavelo
    Aug 2, 2013 at 17:28
  • 3
    +1 for the double underscore __ scheme for naming callback functions, as then the functions become inaccessible and return 404. More so, because private functions are not working for callbacks.
    – user216084
    Mar 13, 2015 at 7:10
12

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]'); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.