我制作了一个custom_form_validation.php
文件,application\libraries
其中包含:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Custom_form_validation extends CI_Form_validation {
function Custom_form_validation()
{
parent::__construct();
}
/* at_least_one_letter() by Ben Swinburne
* http://stackoverflow.com/a/9218114/1685185
-----------------------------------------------*/
public function has_at_least_one_letter( $string )
{
$result = preg_match('#[a-zA-Z]#', $string);
if ( $result == FALSE ) $this->set_message('has_at_least_one_letter', 'The %s field must have at least one letter.');
return $result;
}
}
然后我将它加载到特定的控制器中:
$this->load->library('form_validation');
$this->load->library('custom_form_validation');
最后,我将该函数has_at_least_one_letter
用作:
$this->form_validation->set_rules('FieldName', 'field name', 'has_at_least_one_letter');
我不知道出了什么问题,因为我按照 SO 中给出的示例构建了我自己的库,特别是关于“扩展form_validation
”的库。我错过了一个步骤或一些特殊的部分吗?