1

I am using CodeIgniter 2.3.1 and created a form_validation.php file in config and the content is as below.

<?php
  $config = array(
array(
    'field' => 'firstname',
    'label' => 'First Name',
    'rules' => 'required'
),
array(
    'field' => 'lastname',
    'label' => 'Last Name',
    'rules' => 'required'
),
array(
    'field' => 'email',
    'label' => 'Email',
    'rules' => 'required|valid_email|callback_unique_email'
),
array(
    'field' => 'password',
    'label' => 'Password',
    'rules' => 'required|matches[confirm_password]'
),
array(
    'field' => 'confirm_password',
    'label' => 'Confirm Password',
    'rules' => 'required'
)
 );

function unique_email($email) {
if($email == '[email protected]') {
    $this->form_validation->set_message('unique_email', 'Hello World !');
    return false;
}
 }

?>

And checking the form_validation in register function of user controller. The code is below.

public function register() {
    $this->load->helper('form');
    $data['message'] = '';
    if($this->input->post('submit')) {
        $this->load->library('form_validation');
        if($this->form_validation->run() == FALSE) {
            $data['message'] = 'User could not be saved.';
        } else {
            $user_data['firstname'] = $this->input->post('firstname');
            $user_data['lastname'] = $this->input->post('lastname');
            $user_data['email'] = $this->input->post('email');
            $user_data['password'] = md5($this->input->post('password'));
            if($this->user_model->insert($user_data)) {
                if($this->user_model->login($user_data)) {
                    $this->session->set_flashdata('message', 'User saved successfully.');
                    redirect('/user', 'refresh');
                }
            }
        }
    } 

    $this->load->view('user/register', $data);
}

But I am not getting validation message for the custom method. Please suggest me how to do it?. The work is more appreciated.

2
  • 1
    the custom function belongs in the controller of the function with the form in it. Commented Mar 12, 2013 at 14:42
  • Look at Spikers answer below and read manual. Know that eventually you can also put your form validation in the model, and the database functions in the model, and then just return true/false to the controller. if false, all the error message etc functionality of form validation will still work, it does not have to be in the controller. Commented Mar 12, 2013 at 18:03

2 Answers 2

3

Have a look at the following documentation: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks

As you can see int the documentation, the custom validation function actually belongs in the controller, and not in the config file. By moving the validation function to the controller, the callback function should start getting called.

Another fun fact, people can access this unique_email function through a url (ie. http://yoursite.com/index.php/user/unique_email). To avoid this, we can write the function as a private function by simply placing an underscore at the beginning of the function, like so:

function _unique_email($email) {
    ...
}

You can then call the function in your validation by using the new function name in your config (notice the extra underscore in the callback:

array(
    'field' => 'email',
    'label' => 'Email',
    'rules' => 'required|valid_email|callback__unique_email'
)

In the end, your controller should look similar to the following:

class User extends CI_Controller {

    public function register() {
        $this->load->helper('form');
        $data['message'] = '';
        if($this->input->post('submit')) {
            $this->load->library('form_validation');
            if($this->form_validation->run() == FALSE) {
                $data['message'] = 'User could not be saved.';
            } else {
                $user_data['firstname'] = $this->input->post('firstname');
                $user_data['lastname'] = $this->input->post('lastname');
                $user_data['email'] = $this->input->post('email');
                $user_data['password'] = md5($this->input->post('password'));
                if($this->user_model->insert($user_data)) {
                    if($this->user_model->login($user_data)) {
                        $this->session->set_flashdata('message', 'User saved successfully.');
                        redirect('/user', 'refresh');
                    }
                }
            }
        } 

        $this->load->view('user/register', $data);
    }

    function _unique_email($email) {
        if($email == '[email protected]') {
            $this->form_validation->set_message('unique_email', 'Hello World !');
            return false;
        }
    }

}

Your config would look similar to the following:

$config = array(
    array(
        'field' => 'firstname',
        'label' => 'First Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'lastname',
        'label' => 'Last Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'email',
        'label' => 'Email',
        'rules' => 'required|valid_email|callback__unique_email'
    ),
    array(
        'field' => 'password',
        'label' => 'Password',
        'rules' => 'required|matches[confirm_password]'
    ),
    array(
        'field' => 'confirm_password',
        'label' => 'Confirm Password',
        'rules' => 'required'
    )
);
Sign up to request clarification or add additional context in comments.

Comments

1

I used this answer and got error:

Unable to access an error message corresponding to your field name.

In function _unique_email instead set_message('unique_email', 'Hello World !'); should be set_message('_unique_email', 'Hello World !'); like this:

function _unique_email($email) {
    if($email == '[email protected]') {
        $this->form_validation->set_message('_unique_email', 'Hello World !');
        return false;
    }
}

1 Comment

This should be in the comments section because it does not answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.