2

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;
}
wilsmex
  • 143
  • 10

2 Answers2

1

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'] !== ''; });

mark
  • 21,691
  • 3
  • 49
  • 71
  • 2
    Mark, that is what I already have setup. I'm returning true (for testing) in my custom function, and the field still fails validation when nothing is entered (the custom validator function isn't even called, unless the user inputs some data). I want to be able to determine in my custom function whether the field is allowed to be empty or not. I also tried the allowBlank, but get a 'method not defined'. Any other thoughts? – wilsmex May 12 '16 at 15:48
  • 1
    any other thoughts? I can't seem to figure this one out. – wilsmex May 17 '16 at 20:26
  • 1
    I am desperate for the answer to this question as well @wilsmex. After spending hours now fighting with the validator I am starting thing this simply a bug in Cake and it's frustrating that there's so little attention to these questions. – theraccoonbear May 24 '16 at 19:14
  • You are right, I just confirmed that this seems to be a bug in the system. Can you please open a ticket for it in https://github.com/cakephp/cakephp/issues ? We can then fix it asap. – mark May 26 '16 at 09:06
  • Well, maybe not a bug itself, but sure undesired behavior in your use case. The problem is that once you have a custom validation rule the field is required to be non blank. I updated the answer. – mark May 26 '16 at 09:35
0

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.

jmelero
  • 83
  • 6