php 创建自定义 codeigniter 验证规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8181779/
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
Creating a custom codeigniter validation rule
提问by hairynuggets
I have a function in my login form that checks if the email and password match the values in the database and if so it logs the user into the system.
我的登录表单中有一个函数,用于检查电子邮件和密码是否与数据库中的值匹配,如果匹配,则将用户登录到系统中。
I would like to display a validation error if this function returns false.
如果此函数返回 false,我想显示验证错误。
My problem is that I am unsure how to go about creating this. The message relates to both the password and email fields so I would not want a rule for each input field just display a single message.
我的问题是我不确定如何创建它。该消息与密码和电子邮件字段相关,因此我不希望每个输入字段的规则只显示一条消息。
I have tried using flashdata to achieve this but it only works when the page has been refreshed.
我曾尝试使用 flashdata 来实现这一点,但它仅在页面刷新时才有效。
How can I created a new validation rule solely for the function $this->members_model->validate_member()
??
我怎样才能为函数创建一个新的验证规则$this->members_model->validate_member()
??
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('email_address', '"Email address"', 'trim|required|valid_email');
$this->form_validation->set_rules('password', '"Password"', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$viewdata['main_content'] = 'members/login';
$this->load->view('includes/template', $viewdata);
}
else
{
if($this->members_model->validate_member())
{
回答by Damien Pirsy
You use the callback_
in your rules, see callbacks, for ex.
您callback_
在规则中使用,例如,请参阅callbacks。
$this->form_validation->set_rules('email_address', '"Email address"', 'trim|required|valid_email|callback_validate_member');
and add the method in the controller. This method needs to return either TRUE or FALSE
并在控制器中添加该方法。此方法需要返回 TRUE 或 FALSE
function validate_member($str)
{
$field_value = $str; //this is redundant, but it's to show you how
//the content of the fields gets automatically passed to the method
if($this->members_model->validate_member($field_value))
{
return TRUE;
}
else
{
return FALSE;
}
}
You then need to create a corresponding error in case the validation fails
然后您需要创建相应的错误以防验证失败
$this->form_validation->set_message('validate_member','Member is not valid!');
回答by BoCyrill
One best way to achieve this is extending CodeIgniter's Form Validation library. Let say we want to create a custom validator named access_code_unique
for the field access_code
of the database table users
.
实现这一目标的最佳方法是扩展 CodeIgniter 的表单验证库。假设我们要创建一个以数据库表access_code_unique
字段命名的自定义验证器。access_code
users
All you have to do is creating a Class file named MY_Form_validation.php
in application/libraries
directory. The method should always return TRUE
OR FALSE
您所要做的就是创建一个MY_Form_validation.php
在application/libraries
目录中命名的类文件。该方法应始终返回TRUE
ORFALSE
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
protected $CI;
public function __construct() {
parent::__construct();
// reference to the CodeIgniter super object
$this->CI =& get_instance();
}
public function access_code_unique($access_code, $table_name) {
$this->CI->form_validation->set_message('access_code_unique', $this->CI->lang->line('access_code_invalid'));
$where = array (
'access_code' => $access_code
);
$query = $this->CI->db->limit(1)->get_where($table_name, $where);
return $query->num_rows() === 0;
}
}
Now you can easily add your new created rule
现在您可以轻松添加新创建的规则
$this->form_validation->set_rules('access_code', $this->lang->line('access_code'), 'trim|xss_clean|access_code_unique[users]');