Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

php - Adding custom callback to Codeigniter Form Validation

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. Any I appreciate any help.

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...