>i 有一个名为 website_check 的函数
function website_check($url){
if ($url !=""){
if (preg_match("#^https?://.+#", $url) and fopen($url,"r")){
return TRUE;
}else{
$this->form_validation->set_message('Website url', 'The %s field is invalid');
return FALSE;
}
}else{
$this->form_validation->set_message('Website url', 'The %s field is required');
return FALSE;
}
}
我将此功能用作自定义代码点火器表单验证函数
$this->form_validation->set_rules('website', 'Website', 'callback_website_check');
我在每个控制器中都使用此函数,因此我想将此功能添加到代码点火器表单验证类中并用作默认验证函数。 是否可以将您的函数添加到代码点火器表单验证类中,如果可以这样做?
是的。在应用程序/库目录中创建一个名为 MY_Form_validation.php 的文件。使类名也MY_Form_validation。请确保它扩展CI_Form_validation,并调用父构造函数。然后,将规则添加为方法:
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function website_check($url)
{
if ($url != "") {
if (preg_match("#^https?://.+#", $url) and fopen($url,"r")) {
return TRUE;
} else {
return FALSE;
}
}else{
return FALSE;
}
}
}
您还需要将规则添加到form_validation_lang.php文件(在应用程序/语言/en 中)。只需在底部添加一个规则,如下所示:
$lang['website_check'] = "The %s field is invalid.";
如果该文件不存在,您可以从系统/语言文件夹中复制它。您不应编辑系统文件夹中的文件,因为它们将在更新时被覆盖。
编辑文件"system/libraries/Form_validation.php"并将这个新函数插入到类"CI_Form_validation"中。
function website_check($url){
if (preg_match("#^https?://.+#", $url) and fopen($url,"r")){
return TRUE;
}else{
return FALSE;
}
}
然后编辑文件"语言/英语/form_validation_lang.php"并附加此项:
$lang['website_check'] = "The %s field is invalid";
然后将其用作:
$this->form_validation->set_rules('website', 'Website', 'website_check');