我想验证Bank Account number(123456789)
和Routing number(434344343)
,
自定义message=>Please enter 12 digit valid account number
。(000123456789)
我使用了数字验证器,整数但不能正常工作。 验证器应检查值是数字还是数字长度。 我在这里查看此文档tutorial-core-validators
public function rules()
{
return [
[['accountnumber'], 'number', 'min' => 12, 'max'=>12],// not wokred
[['routingnumber'], 'number', 'min' => 9, 'max'=>9], // not wokred
// then I used
[['accountnumber', 'routingnumber', ], 'integer']
]
}
任何建议?
答案 0 :(得分:4)
min
和max
参数用于值,而不是长度,也许您应该保存为字符串。
['accountnumber', 'string', 'length' => [12, 12]
并添加正则表达式:
['accountnumber', 'match', 'pattern' => '/^[0-9]*$/i']
答案 1 :(得分:1)
试试这个:
public function rules()
{
return [
[['accountnumber'], 'string', 'min' => 12, 'max'=>12, 'message' => "Please enter 12 digit valid account number"],
[['routingnumber'], 'string', 'min' => 9, 'max'=>9],
[['accountnumber', 'routingnumber', ], 'integer'],
]
}
答案 2 :(得分:1)
最小,最大数量验证
public function rules(){
return [
[['accountnumber'],'number','min'=>10],
[['accountnumber'],'number','max'=>100],
[['accountnumber'],'number','min'=>10,'max'=>100],
];
}
最小,最大字符串验证
public function rules(){
return [
[['min_string'],'string','min'=>10],
[['max_string'],'string','max'=>10],
[['min_max_string'],'string','min'=>5,'max'=>10],
['min_max_string2', 'string', 'length' => [4, 10]],
];
}
自定义验证
public function rules(){
return [
['custom_validation','custom_function_validation', 'values'=>['One', 'Two']],
];
}
public function custom_function_validation($attribute, $params){
// add custom validation
if (!in_array($this->$attribute, $params['values']))
$this->addError($attribute,'Custom Validation Error');
}