您可以通过创建以添加额外的验证规则来扩展 form_validation 类以获得最大控制application/libraries/MY_form_validation.php
- 我在下面附上了一个示例。
直接编辑系统库是不好的做法;CI 提供了更好的选择(通过类、库、钩子等覆盖/自定义MY_
)。这为您提供了轻松升级 CI 版本的好处,并使您的应用程序可移植/自定义代码与核心框架隔离。
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter Form Validation Extension
*/
class MY_Form_validation extends CI_Form_validation {
/**
* MY_Form_validation::valid_url
* @abstract Ensures a string is a valid URL
*/
function valid_url($url) {
if(preg_match("/^http(|s):\/{2}(.*)\.([a-z]){2,}(|\/)(.*)$/i", $url)) {
if(filter_var($url, FILTER_VALIDATE_URL)) return TRUE;
}
$this->CI->form_validation->set_message('valid_url', 'The %s must be a valid URL.');
return FALSE;
}
/**
* MY_Form_validation::alpha_extra()
* @abstract Alpha-numeric with periods, underscores, spaces and dashes
*/
function alpha_extra($str) {
$this->CI->form_validation->set_message('alpha_extra', 'The %s may only contain alpha-numeric characters, spaces, periods, underscores & dashes.');
return ( ! preg_match("/^([\.\s-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
}
/**
* MY_Form_validation::numeric_comma()
* @abstract Numeric and commas characters
*/
function numeric_comma($str) {
$this->CI->form_validation->set_message('numeric_comma', 'The %s may only contain numeric & comma characters.');
return ( ! preg_match("/^(\d+,)*\d+$/", $str)) ? FALSE : TRUE;
}
/**
* MY_Form_validation::matches_pattern()
* @abstract Ensures a string matches a basic pattern
*/
function matches_pattern($str, $pattern) {
if (preg_match('/^' . $pattern . '$/', $str)) return TRUE;
$this->CI->form_validation->set_message('matches_pattern', 'The %s field does not match the required pattern.');
return FALSE;
}
}
/* End of file MY_form_validation.php */
/* Location: ./{APPLICATION}/libraries/MY_form_validation.php */