CakePHP - validation can be empty but if not empty need to be at least 4 character & numeric

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

12
задан teepusink 6 May 2011 в 15:14
поделиться