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);
halfer
  • 19,824
  • 17
  • 99
  • 186
shin
  • 31,901
  • 69
  • 184
  • 271

3 Answers3

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.";

Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
  • 1
    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. – Rick Calder Oct 14 '12 at 09:18
  • @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. – Jordan Arsenault Oct 14 '12 at 17:11
  • 1
    It does actually work, I do it on our register page. $this->form_validation->set_rules('referralTypeId','Referral','callback__checkReferral'); – Rick Calder Oct 14 '12 at 20:36
  • 1
    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_` – tim peterson Apr 01 '13 at 22:02
  • How to load model in form_validation library. – user254153 Jun 11 '16 at 03:02
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;
    }
}  
Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53
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.

reddy
  • 1,721
  • 3
  • 16
  • 26