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;
}
]);
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.
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.
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.