6

I want to limit my registration to emails with @mywork.com I made the following in My_Form_validation:

public function email_check($email)
    {
        $findme='mywork.com';
        $pos = strpos($email,$findme);
        if ($pos===FALSE)
        {
            $this->CI->form_validation->set_message('email_check', "The %s field does not have our email.");
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

I use it as follows. I use CI rules for username and password and it works, for email it accepts any email address.

function register_form($container)
    {
....
....

/ Set Rules
$config = array(
...//for username
// for email            
    array(
  'field'=>'email',
  'label'=>$this->CI->lang->line('userlib_email'),
  'rules'=>"trim|required|max_length[254]|valid_email|callback_email_check|callback_spare_email"
   ),
...// for password
 );

$this->CI->form_validation->set_rules($config);
2
  • Where is the actual callback function? Commented Oct 14, 2012 at 2:54
  • In My_Form_validation. But I added to the same file as function register_form($container). But it does not work. Commented Oct 14, 2012 at 3:05

3 Answers 3

18

The problem with creating a callback directly in the controller is that it is now accessible in the url by calling http://localhost/yourapp/yourcontroller/yourcallback which isn't desirable. There is a more modular approach that tucks your validation rules away into configuration files. I recommend:

Your controller:

<?php
class Your_Controller extends CI_Controller{
    function submit_signup(){
        $this->load->library('form_validation');
        if(!$this->form_validation->run('submit_signup')){
            //error
        }
        else{
            $p = $this->input->post();
            //insert $p into database....
        }
    }
}

application/config/form_validation.php:

<?php
$config = array
(   
    //this array key matches what you passed into run()
    'submit_signup' => array
    (
        array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'required|max_length[255]|valid_email|belongstowork'
        )
        /*
        ,
        array(
            ...
        )
        */

    )
    //you would add more run() routines here, for separate form submissions.
);

application/libraries/MY_Form_validation.php:

<?php
class MY_Form_validation extends CI_Form_validation{    
     function __construct($config = array()){
          parent::__construct($config);
     }
     function belongstowork($email){
         $endsWith = "@mywork.com";
         //see: http://stackoverflow.com/a/619725/568884
         return substr_compare($endsWith, $email, -strlen($email), strlen($email)) === 0;
     }
}

application/language/english/form_validation_lang.php:

Add: $lang['belongstowork'] = "Sorry, the email must belong to work.";

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

5 Comments

Simply naming the validation function with an underscore as the first character stops it from being directly accessible through a browser, although your solution makes it universally accessible which in most cases makes perfect sense. Just wanted to note that for those rare occasions where you really only need it once. so naming the function _belongstowork means it's only internally accessible to the application.
@RickCalder yes, I'm familiar with that, but for form validation functions you must use a function called callback_thefunction so that will double-up on underscores;one for the callback, and one for preventing http access: callback__belongstowork, and (I haven't tested it, but), I don't think CodeIgniter internally will match this regex.
It does actually work, I do it on our register page. $this->form_validation->set_rules('referralTypeId','Referral','callback__checkReferral');
Just for everyone's FYI, one thing that tripped me up as I tried to extend the form validation library using MY_Form_validation, is to get rid of the callback_ in the ->form_validation->set_rules(...) since it is no longer a callback within a controller. Please notice this in @JordanArseno's answer above where belongstowork is NOT prefaced with callback_
How to load model in form_validation library.
0

Are you need validation something like this in a Codeigniter callback function?

$this->form_validation->set_rules('email', 'email', 'trim|required|max_length[254]|valid_email|xss_clean|callback_spare_email[' . $this->input->post('email') . ']');

if ($this->form_validation->run() == FALSE)
{
    // failed
    echo 'FAIL';
}
else
{ 
    // success
    echo 'GOOD';
}

function spare_email($str)
{

    // if first_item and second_item are equal
    if(stristr($str, '@mywork.com') !== FALSE)
    {
    // success
    return $str;
    }
    else
    {
    // set error message
    $this->form_validation->set_message('spare_email', 'No match');

    // return fail
    return FALSE;
    }
}  

Comments

0

A correction to Jordan's answer, the language file that you need to edit should be located in

system/language/english/form_validation_lang.php

not application/.../form_validation_lang.php. If you create the new file under the application path with the same name, it will overwrite the original in the system path. Thus you will lose all the usage of the original filters.

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.