Arjan
Arjan

Reputation: 11

CakePHP 3.5.1 Validation

I have the following question. I have for example 2 inputfields, when field A has a value of "Project done" I want to force that field B must be filled with a certain value let say "Arjan"

Does anybody has a solution for me of how to accomplish this.

Sorry for my poor English, but thanks in advance.

Upvotes: 1

Views: 61

Answers (2)

harpax
harpax

Reputation: 6106

You can create a custom validation rule:

$validator->add('fieldB', 'custom', [
    'rule' => function ($value, $context) {
        if ($context['data']['fieldA'] == "Project done") {
            return $value == "Arjan"
        }
        return true;
    },
    'message' => 'Error message'
]);

Note that the format of $context might be a little different

Upvotes: 1

norrin
norrin

Reputation: 171

Looks like this should be done in javascript(or jquery) as field A is dynamically changed by the user. A simple logic perhaps:

   $("#field_a").on("change", function(){
      var field_a = $("#field_a").val();
      //if true
      if(field_a == "done"){
        //preferrably, this value should be coming from an associative array.
        $("#field_b").val("Arjan"); 
      }
   });

Upvotes: 0

Related Questions