Php 如何在CodeIgniter的表单验证中设置自定义过滤器?
我用的是CodeIgniter的。以下是一个例子:Php 如何在CodeIgniter的表单验证中设置自定义过滤器?,php,codeigniter,Php,Codeigniter,我用的是CodeIgniter的。以下是一个例子: $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); 文件说: 任何接受一个参数的本机PHP函数都可以用作 规则,如htmlspecialchars、trim、MD5等 如果我希望该值通过我自己的自定义过滤器,该怎么办?例如,我希望清除该值中的“badWord” CodeIgniter已经提供了进行自定义验证的方法,但没有
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
文件说:
任何接受一个参数的本机PHP函数都可以用作
规则,如htmlspecialchars、trim、MD5等
如果我希望该值通过我自己的自定义过滤器,该怎么办?例如,我希望清除该值中的“badWord”
CodeIgniter已经提供了进行自定义验证的方法,但没有提供自定义过滤器。自定义验证只返回true或false,而不返回筛选后的字符串
function isPolite($string = '') {
if (strpos($string, 'badWord') !== false) {
$this->form_validation->set_message(
'fieldName',
'contains a very bad word'
);
return false;
} else {
return true;
}
}
Jojo,你一定错过了用户指南,它叫做
回调
,这是
例如:
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
Jojo,你一定错过了用户指南,它叫做
回调
,这是
例如:
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
Codeigniter有一个助手,可以在RPM下过滤出“坏话”@RPM如何将其与表单验证集成?另外,请记住,我需要更多的自定义过滤器,而不仅仅是删除诅咒词。你需要过滤哪些词吗?或者系统是否需要能够决定是否应该对单词进行审查?@RPM清除诅咒词只是一个例子。在我的实际应用程序中,我没有必要删除诅咒词。相反,我有一些其他复杂的过滤器——通过分析语言来删除垃圾邮件的过滤器。我的根本问题不是如何去除咒语。我需要在CodeIgniter的表单验证中附加一些自定义过滤器。CodeIgniter有一个助手,可以在RPM下过滤出“坏词”。我如何将其与表单验证集成?另外,请记住,我需要更多的自定义过滤器,而不仅仅是删除诅咒词。你需要过滤哪些词吗?或者系统是否需要能够决定是否应该对单词进行审查?@RPM清除诅咒词只是一个例子。在我的实际应用程序中,我没有必要删除诅咒词。相反,我有一些其他复杂的过滤器——通过分析语言来删除垃圾邮件的过滤器。我的根本问题不是如何去除咒语。我需要在CodeIgniter的表单验证中附加一些自定义过滤器。@JoJo,好吧,验证之后,您可以做任何您想做的事情,确保从$this->input->post('value')获取值
@JoJo,那么您应该在验证之后做任何您喜欢的事情,确保从$this->input->post('value')获取值代码>