如何创建允许字段为空的验证规则,但如果不是,则需要为数字且长度为4个字符?
这就是我现在拥有的
'year' => array(
'numeric' => array(
'rule' => 'numeric',
'message' => 'Numbers only'
),
'maxLength' => array(
'rule' => array('maxLength', 4),
'message' => 'Year in YYYY format'
),
'minLength' => array(
'rule' => array('minLength', 4),
'message' => 'Year in YYYY format'
)
)
Run Code Online (Sandbox Code Playgroud)
这很好用但是当字段为空时,它仍然运行验证.
谢谢,
Tee
dho*_*tet 27
以下代码片段可以解决这个问题:
'numeric' => array(
'rule' => 'numeric',
'allowEmpty' => true,
'message' => 'Numbers only'
),
Run Code Online (Sandbox Code Playgroud)
另请参阅食谱中有关数据验证的章节.
归档时间: |
|
查看次数: |
13098 次 |
最近记录: |