php 向 Codeigniter 表单验证添加自定义回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12878863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding custom callback to Codeigniter Form Validation
提问by shin
I want to limit my registration to emails with @mywork.com I made the following in My_Form_validation.
我想将我的注册限制为使用@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;
}
}
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.
我使用它如下。我对用户名和密码使用 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);
回答by Jordan Arseno
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/yourcallbackwhich isn't desirable. There is a more modular approach that tucks your validation rules away into configuration files. I recommend:
直接在控制器中创建回调的问题在于,现在可以通过调用在 url 中访问它,http://localhost/yourapp/yourcontroller/yourcallback这是不可取的。有一种更模块化的方法可以将您的验证规则隐藏到配置文件中。我建议:
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:
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;
}
}
application/language/english/form_validation_lang.php:
application/language/english/form_validation_lang.php:
Add: $lang['belongstowork'] = "Sorry, the email must belong to work.";
添加: $lang['belongstowork'] = "Sorry, the email must belong to work.";
回答by Marin Sagovac
Are you need validation something like this in a Codeigniter callback function?
您是否需要在 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;
}
}
回答by reddy
A correction to Jordan's answer, the language file that you need to edit should be located in
更正乔丹的答案,您需要编辑的语言文件应位于
system/language/english/form_validation_lang.php
系统/语言/英文/form_validation_lang.php
not application/.../form_validation_lang.php. If you create the new file under the application path with the same name, it will overwrite the original in the system path. Thus you will lose all the usage of the original filters.
不是 application/.../form_validation_lang.php。如果在同名的应用程序路径下创建新文件,它将覆盖系统路径中的原始文件。因此,您将失去原始过滤器的所有用法。

