Q:

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

A:

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.

