将自定义回调添加到Codeigniter表单验证 [英] Adding custom callback to Codeigniter Form Validation

查看:112
本文介绍了将自定义回调添加到Codeigniter表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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


推荐答案

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

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:

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



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



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;
     }
}



/form_validation_lang.php



添加: $ lang ['belongsstowork'] =对不起,电子邮件必须属于to work。;

这篇关于将自定义回调添加到Codeigniter表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆