0
votes

I have a numeric input field that is allowed to be empty, but if is not empty I'd like to return a validation error if letters are entered for example.

at the moment if I removed allow empty validation works just fine for both numeric and notEmpty but this field is optional how can I fix this?

Here is the validation on my model:

'tickets' => array(
    'numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter only numbers',
        'allowEmpty' => true,
    ),
),

once again if I set allowEmpty to false this works as expected. I've been playing around with it by separating the rules, but so far no luck. Any help is appreciated.

1

1 Answers

1
votes

You specify "allowed to be empty" and "optional" in a way that leads me to think that what you're after is actually required, which you should set to false.

required => true does not mean the same as the validation rule notEmpty(). required => true indicates that the array key must be present - it does not mean it must have a value. Therefore validation will fail if the field is not present in the dataset, but may (depending on the rule) succeed if the value submitted is empty (‘’).

So

'tickets' => array(
    'numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter only numbers',
        'allowEmpty' => true,
        'required' => false,
    )
),

would mean that it is ok for the field to be "optional" (not included in the returned array), if it is included it is "allowed to be empty" and if it isn't the rule is "numeric".