1

I'm trying to do an email validation whereby the domain of the email would be @abc123.com. I've separated my form validation rules into another file in the application/config folder called form_validation.php. One of my rules consists of a callback_email_check.

Where should I put the function? In the main controller or together with the form_validation.php file where all my form validation rules are? I've tried putting at both options but at where I display my error message I'm getting an output saying Unable to access an error message corresponding to your field name Email.(email_check).

function email_check($email)
{
    if( strpos($email, '@abc123.com') !== FALSE ) return TRUE;

    $this->form_validation->set_message('email', 'Please use abc123 email only.');

    return FALSE;
}

form_validation.php

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

/* Form Validation Rules */

$config = array(
    'login' => array(
            array(
                    'field' => 'user_id',
                    'label' => 'User ID',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'password',
                    'label' => 'Password',
                    'rules' => 'trim|required'
            )
    ),
    'sign_up' => array(
            array(
                    'field' => 'user_id',
                    'label' => 'User ID',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'name',
                    'label' => 'Name',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'trim|required|valid_email|callback_email_check'
            ),
            array(
                    'field' => 'department',
                    'label' => 'Department',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'password',
                    'label' => 'Password',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'cfm_password',
                    'label' => 'Re-type Password',
                    'rules' => 'trim|required|matches[password]'
            )
    ),
    'edit_profile' => array(
            array(
                    'field' => 'new_password',
                    'label' => 'New Password',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'retype_password',
                    'label' => 'Re-type Password',
                    'rules' => 'trim|required|matches[new_password]'
            )
    ),
    'forgot_password' => array(
            array(
                    'field' => 'user_id',
                    'label' => 'User ID',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'trim|required|valid_email|callback_email_check'
            )
    )
);
?>
5
  • Callback functions are placed within the same controllers from which they are called in form validation. Commented Sep 29, 2015 at 3:27
  • @AnmolRaghuvanshi I've tried that, but it returns the same error. Commented Sep 29, 2015 at 3:31
  • will you post your set_rule part also Commented Sep 29, 2015 at 3:31
  • @AnmolRaghuvanshi Then that would ignore the error, I'm trying to set a rule to make sure the user only uses '@abc123.com' as the domain. Commented Sep 29, 2015 at 3:38
  • @AnmolRaghuvanshi Tried it already, like I said it ignores the check for the domain since it'll always be TRUE. Commented Sep 29, 2015 at 3:41

3 Answers 3

1

On your function email_check, the set_message is not correct it should be the same name as the function.

Change this

$this->form_validation->set_message('email', 'Please use abc123 email only.');

To

$this->form_validation->set_message('email_check', 'Please use abc123 email only.');

Call backs http://www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks

Sign up to request clarification or add additional context in comments.

2 Comments

I assumed it was the name, which was wrong. Thank you.
Not working get same error any suggestion @Wolfgang1983
0

I am also facing the same problem and this is how i resolved it...

You can put email_check function in same controller. In case you are not getting the error message in callback then pass $this in your run()

if ($this->form_validation->run($this)) { ...}

and associating a Controller Method with a Rule Group -

$config = array( 'controller/method' => array(...));

view link for more : [associating a Controller Method with a Rule Group][1]

cheers !!

Comments

0

Just add this line in your config:

array(
      'field' => 'email',
      'label' => 'Email',
      'rules' => 'trim|required|valid_email|callback_email_check',
       **'errors' => array('email_check' => 'Your Error Message')**
),

Comments

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.