I'm trying to use the comparison rule but seems am missing something or not doing it right. Basically what I need is validation to check that the value of small_size is less or equal to big_size.
But this isn't working, am currently getting the error regardless of the sizes.
In the Table:
...
$validator
->add('small_size', 'valid', ['rule' => 'numeric'])
->requirePresence('small_size', 'create')
->notEmpty('small_size')
->add('small_size', 'comparison', [
'rule' => ['comparison', 'big_size', '<='],
'message' => 'Small size cannot be more than the Big size.'
]);
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['Entity_id'], 'Entity'));
return $rules;
}
Some help on this will be much appreciated.
You cannot use the built-in comparison
rule to compare two fields, you need to use a custom rule:
$validator->add('small_size', 'comparison', [
'rule' => function ($value, $context) {
return intval($value) <= intval($context['data']['big_size']) ;
},
'message' => 'Small size cannot be bigger than Big size.'
]);