Maxlength validation with condition

Hey guys,

i need a maxlength validation rule with a condition like this (described here Validation - 4.x):

            $validator->add('text', [
                'maxLength' => [
                    'rule' => ['maxLength', 10],
                    'message' => 'The value is too long.',
                    'on' => function ($context) {
                        return $this->isTypeA($context['data']['tenant_id']);
                    }
                ],
            ])

In this solution, my input field always has a maxlength of 10, even when I return false in the on function.

How can i set the maxlength, based on a value in data model (here a type of tenant)?

can you elaborate more?

Correct me if I am misunderstanding but can you just use a rule and a function you can use a query to get hold of a related model / entity to check against.


     ->add('text', 'myMaxLengthButTheWayIWantIt', [
                'rule' => function($value, $context) {
                    $table = $context['providers']['table'];

                    $tenantId = $context['data']['tenant_id'];

                    $tenant = $table->Tenant->get($tenantId);
                   
                    // use tenant data to validate whatever

                    if($isFail) {
                        return "You can't go forward while this isFail";
                    }

                     //  any number of tests return either a string or false

                    // if all is OK return true
                    return true;
                }
            ]);
1 Like

That is a solution, but this validation should only be done server-side, not on the client side. Perhaps only the maxLength rule is being handled on the client side because the form helper only generates the maxlength attribute with that rule, right?

Please don’t ask me why we want to do it this way. :smiley:

We have two ‘on-premise’ applications that retrieve data from the CakePHP application via REST API. In one application, we have only 5 characters available for the field, while in the other application, we have 15 characters. Expanding the field in Application A is not possible, so we need to ensure that only 5 characters can be entered in the CakePHP application, whereas 15 characters can be entered for the other application.

If you need any further informations, just let me know!

The solution you were provided with above is server side. Not sure why you’d think it’s not. The standard length validator can’t do what you want, so you have to use a custom validation function. That’s what’s been provided for you here.

I said, using a validation rule like @jmcd73 described is done server-side, only when you patch the entity in PHP, right?
A validation rule like “maxLength” generates the maxlength attribute <input maxlength=5 />, so this validation is client-side AND server-side. The browser checks the maxlength! You can manipulate that maxlength attribute with the browsers developer console, so you get finally a validation error from server.

Ah, I understand now.

When you generate the input control for this, in your template, just include 'maxlength' => $length in the options you pass, where $length is whatever is applicable.