【问题标题】:Adding custom callback to Codeigniter Form Validation将自定义回调添加到 Codeigniter 表单验证
【发布时间】:2012-10-04 10:30:09
【问题描述】:

我想将我的注册限制在@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);

【问题讨论】:

  • 真正的回调函数在哪里?
  • 在 My_Form_validation 中。但我添加到与函数 register_form($container) 相同的文件中。但它不起作用。

标签: php codeigniter validation


【解决方案1】:

直接在控制器中创建回调的问题在于,现在可以通过调用 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.";

【讨论】:

  • 只需使用下划线作为第一个字符来命名验证函数即可阻止它通过浏览器直接访问,尽管您的解决方案使其普遍可访问,这在大多数情况下非常有意义。只是想指出,对于那些你真的只需要一次的罕见场合。因此将函数命名为 _belongstowork 意味着它只能在应用程序内部访问。
  • @RickCalder 是的,我对此很熟悉,但是对于表单验证函数,您必须使用一个名为 callback_thefunction 的函数,以便下划线加倍;一个用于回调,一个用于防止 http 访问:callback__belongstowork,并且(我还没有测试过,但是),我认为 CodeIgniter 内部不会匹配这个正则表达式。
  • 它确实有效,我在我们的注册页面上做了。 $this->form_validation->set_rules('referralTypeId','Referral','callback__checkReferral');
  • 仅供大家参考,当我尝试使用MY_Form_validation 扩展表单验证库时,让我感到困惑的一件事是去掉-&gt;form_validation-&gt;set_rules(...) 中的callback_,因为它是不再是控制器内的回调。请在上面@JordanArseno 的回答中注意这一点,其中belongstowork NOTcallback_ 开头
  • 如何在 form_validation 库中加载模型。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 2012-03-31
      相关资源
      最近更新 更多