將驗證規則用於CI3中的配置文件時,我無法確定將回調放在哪裏。這裏是我的form_validation.php:將CI3 /驗證規則添加到配置文件並使用回調
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'blog_post' => array(
array(
'field' => 'entry_name',
'label' => 'entry_name',
'rules' => 'min_length[8]|trim|required|max_length[255]'
),
array(
'field' => 'entry_body',
'label' => 'entry_body',
'rules' => 'trim|required|min_length[12]|callback_html_length_without_html'
),
),
);
function html_length_without_html(){
if (2 < 1)
{
$this->form_validation->set_message('html_length_without_html', 'The field can not be the word');
return FALSE;
} else {
return TRUE;
}
}
然而,當我運行上面,我得到以下錯誤:
Unable to access an error message corresponding
to your field name entry_body.(html_length_without_html)
在哪裏放置回調 「html_length_without_html()」?
也許閱讀用戶指南將會給出答案。 https://www.codeigniter.com/userguide3/libraries/form_validation.html#callbacks-your-own-validation-methods – TimBrownlaw
是的,閱讀過,顯然沒有得到它。 – user3264461
那麼那個函數會放在你正在執行實際驗證的控制器中。使用表單驗證配置來配置驗證規則的相同控制器。 – TimBrownlaw