向Codeigniter表单验证添加自定义回调

dzjeubhm  于 2023-03-10  发布在  其他
关注(0)|答案(3)|浏览(120)

我想将我的注册限制为使用@ www.example.com的电子邮件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);
kq0g1dla

kq0g1dla1#

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

您的控制器:

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

lp0sw83n

lp0sw83n2#

您是否需要在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;
    }
}
wz1wpwve

wz1wpwve3#

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

系统/语言/英语/表单验证语言.php

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

相关问题