6

我想将我的注册限制在使用@mywork.com 的电子邮件中,我在 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;
        }
    }

我使用它如下。我对用户名和密码使用 CI 规则,它可以工作,对于电子邮件,它接受任何电子邮件地址。任何我感谢任何帮助。

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);
4

3 回答 3

18

直接在控制器中创建回调的问题在于,现在可以通过调用在 url 中访问它,http://localhost/yourapp/yourcontroller/yourcallback这是不可取的。有一种更模块化的方法可以将您的验证规则放入配置文件中。我建议:

你的控制器:

<?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

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

于 2012-10-14T03:46:36.443 回答
0

您是否需要在 Codeigniter 回调函数中进行类似的验证?

$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;
    }
}  
于 2012-10-14T03:22:35.380 回答
0

对乔丹答案的更正,您需要编辑的语言文件应位于

系统/语言/英文/form_validation_lang.php

不是应用程序/.../form_validation_lang.php。如果在应用程序路径下创建同名的新文件,它将覆盖系统路径中的原始文件。因此,您将失去所有原始过滤器的使用。

于 2014-06-02T20:58:28.150 回答