How do I use a model validation to ensure one field is >= another field in the same model in CakePHP?
-
20-08-2019 - |
Question
I am trying to validate my model, I am using CakePHP 1.2.3.8166 and mysql 5
I have my model definied as it:
<?php
class Actividad extends AppModel {
var $name = 'Actividad';
var $validate = array('maxfield' => array(
'rule'=> array('chkValue'),
'message'=>'i2'
));
function chkValue($data){
return $data["maxfield"]>=$data["minfield"]
}
}
My table has 2 fields; maxfield
& minfield
. I need to validate maxfield
always >= minfield
but I can't figure out how to check minfield
value.
Solution
You can access the value of "minfield" with $this->data['Actividad']['minfield']
OTHER TIPS
you already have the validation for maxfield, you just need to do the same thing with the minfield. so your $validate should be like this:
var $validate= array(
'maxfield' => array(
'rule'=> 'chkValue',
'message'=>'i2'
),
'minfield' => array(
'rule'=> 'chkValue',
'message'=>'i2'
)
);
And by the way. 'rule' => 'nameOfValidationFunction'. no need to put in array.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow