How can I create a validation rule that allows a field to be empty but if it is not, it needs to be numeric and 4 character long?
This is what I have now
'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'
)
)
That works great but when the field is empty, it still run the validation.
Thanks,
Tee
The following snippet should do the trick:
'numeric' => array(
'rule' => 'numeric',
'allowEmpty' => true,
'message' => 'Numbers only'
),
See also the chapter about data validation in the cookbook.
you also forgot the last => true paramater - see http://www.dereuromark.de/2010/07/19/extended-core-validation-rules/ for details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With