I try to validate a simple form with three fields: modus, range_from and range_to. The validation of the fields range_from and range_to are dependent on the value of field modus. The fields range_from and range_to can be empty if field modus = 1. Otherwise they must have a value and range_from has to be a lower number than range_to. My rules work fine, but as soon as I allow the fields to be empty, all other validations are skipped (also when the field is not empty).
UnitsTable.php
public function validationDefault(Validator $validator): Validator {
$validator
->scalar('modus')
->notEmptyString('modus', 'Please select a billing modus');
$validator
->add('range_from', [
'e0' => [
'rule' => function ($value, $context) {
return (empty($value) && in_array($context['data']['modus'], [2, 5, 8])) ? false : true;
},
'message' => 'You have selected the modus "weight range", please enter a minimum weight'
],
'e1' => [
'rule' => function ($value, $context) {
return (!empty($value) && in_array($context['data']['modus'], [2, 5, 8]) && $value > 9999.999) ? false : true;
},
'message' => 'Please enter a weight lower than 9999,999'
],
'e2' => [
'rule' => function ($value, $context) {
return (!empty($value) && in_array($context['data']['modus'], [2, 5, 8]) && !empty($context['data']['range_to']) && $value >= $context['data']['range_to']) ? false : true;
},
'message' => 'Please enter a weight lower than "Range To"',
]
])
->allowEmptyString('range_from');
$validator
->add('range_to', [
'e0' => [
'rule' => function ($value, $context) {
return (empty($value) && in_array($context['data']['modus'], [2, 5, 8])) ? false : true;
},
'message' => 'You have selected the modus "weight range", please enter a maximum weight'
],
'e1' => [
'rule' => function ($value, $context) {
return (!empty($value) && in_array($context['data']['modus'], [2, 5, 8]) && $value > 9999.999) ? false : true;
},
'message' => 'Please enter a weight lower than 9999,999'
],
'e2' => [
'rule' => function ($value, $context) {
return (!empty($value) && in_array($context['data']['modus'], [2, 5, 8]) && !empty($context['data']['range_from']) && $value <= $context['data']['range_from']) ? false : true;
},
'message' => 'Please enter a weight greater than "Range From"',
]
])
->allowEmptyString('range_to');
Unfortunately no validation is executed as long as the "allowEmptyString" rule is there. But when I remove it and the field modus has a value that doesn't need ranges I get the error for the ranges-fields: "This field cannot be left empty". In former cake-versions I was able to add an allowEmpty to every custom rule, is that not possible anymore?
public $validate = [
'modus' => [
'notempty' => [
'rule' => ['notBlank'],
'message' => 'Please select a Modus'
]
],
'range_from' => [
'checkMin' => [
'rule' => ['checkMin'],
'message' => 'You have selected the Modus "Weight Range", please enter a minimum Weight'
],
'maxWeight' => [
'rule' => ['maxWeight', 9999.999],
'message' => 'Please enter a Weight lower than 9999,999',
'allowEmpty' => true
],
'ltRangeTo' => [
'rule' => ['ltRangeTo'],
'message' => 'Please enter a Weight lower than "Weight Range To"',
'allowEmpty' => true
]
],