2

I have a duration field that sometimes can be empty and sometimes can't, depending on the other data sent by the form. So I'm trying to do custom validation in CakePHP3.

In my table I did

public function validationDefault(Validator $validator)
{
    $validator
    ->add('duration', 'durationOk', [
        'rule' => 'isDurationOk',
        'message' => 'duration is not OK',
        'provider' => 'table'
    ]);
    return $validator;
}

public function isDurationOk($value, $context)
{
    // do some logic
    return false; // Always return false, just for test
}

Now when I set the value for duration field I get an 'duration is not OK' error (as expected). But when I let the value empty I get a 'This field cannot be left empty' error.

So I added:

->allowEmpty('duration');

But in this case when duration is empty I don't get an error at all.

Am I doing something wrong or it's just me don't understanding how validation works?

1 Answer 1

2

Let me read the book for you:

Conditional Validation

When defining validation rules, you can use the on key to define when a validation rule should be applied. If left undefined, the rule will always be applied. Other valid values are create and update. Using one of these values will make the rule apply to only create or update operations.

Additionally, you can provide a callable function that will determine whether or not a particular rule should be applied:

'on' => function ($context) {
    // Do your "other data" checks here
    return !empty($context['data']['other_data']);
}

So just define the conditions depending on your "other data" in the callback to apply the rule only when the conditons are true.

Alternatively you can manipulate the plain form data even before it gets validated in the beforeMarshal() callback of the table and change the form data as needed or load another validator or modify the validator.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok thank you very much: I managed to make it work. But there's still something I dont' get: I thought the rules where processed in AND. So that only when all the conditions where true no error was generated. In my mind if I set allowEmpty and a second rule the field can be empty only if the second rule applies. Instead if the field is empty the other rule is not even evaluated. Can you please explain how this works?
book.cakephp.org/3.0/en/core-libraries/… "When fields have multiple rules, each validation rule will be run even if the previous one has failed. This allows you to collect as many validation errors as you can in a single pass. However, if you want to stop execution after a specific rule has failed, you can set the last option to true:" Does that answer it?
be patient with me, but I still don't understand why when I set allowEmpty and pass an empty value my custom rule is not evaluated at all
Because the field is empty and allowed to be empty?
If this is the way it woks, ok. But it still seems counter intuitive to me. I thought the field had to pass all the rules. In this way after checking that the field is empty the validation process stops.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.