wilsmex
wilsmex

Reputation: 143

CakePHP 3.x Custom Validation Field Required

As I'm building form elements dynamically I want to be able to check and see if a form field is required or not via a custom validation rule. The problem is that when I add a custom validation rule, it forces the field to not be empty. If I allow the field to be empty, it doesn't check my custom validator unless something is entered in the field.

How can I check in a callback whether to allow or not a field as required?

In my SubmissionsTable

 public function validationDefault(Validator $validator)
 {
        $validator
        ->add("custom_value_q", [
            "custom" => [
                "rule" => [$this, "customFieldIsRequired"],
                "message" => "Message Here"
                    ]
                ]
            );
     return $validator;
}       

public function customFieldIsRequired($value, $context) 
{
 //logic here 
 return true;
}

Upvotes: 2

Views: 1779

Answers (2)

jmelero
jmelero

Reputation: 93

I know this is a bit old, but I'm facing the same problem, and as I see in github the discussion about it is still open (https://github.com/cakephp/cakephp/issues/8925 and https://github.com/cakephp/cakephp/issues/12484).

In this case, when you have a field that may be empty on some situations (may be if other field was filled), you can do this:

    $validator->allowEmptyString('field_a', function ($context) {
        // check whether the field can or cannot be empty
        return $canBeEmpty;
    });

as this may be incorrectly evaluated when an empty form is built (for new entities) as all fields are empty probably, you may have to add the attribute required => false to the form input, if not the field would be marked as required and ask to be filled mandatory.

While having to instruct the form helper whether the field should or shouldn't be required is far from ideal, it's not a big deal, and works to validate entities and modeless forms as well.

Only for validating entities, according to this (https://github.com/cakephp/cakephp/issues/12484#issuecomment-414465002) you may use application rules, which are evaluated only when the entity is being persisted, so a field can be allowed to be empty in validations and then application rules will be applied anyway.

Upvotes: 0

mark
mark

Reputation: 21743

Returning true in your custom one when empty $value is passed in should do the trick.

If you want the field to allow empty string (= empty), use allowBlank('custom_value_q') on top, logically you don't need to invoke the custom validator function then, that's why it is bypassed in the empty case.

//UPDATE You do, however, have the option to provide a callback for allowEmpty(), with this it should be possible to only invoke the custom validation rule if you really want it (if the field needs to be validated because non blank).

$validator->allowEmpty('fieldname', function ($context) { return !isset($context['data']['description']) || $context['data']['description'] !== ''; });

Upvotes: 1

Related Questions