validation - Validating third variable on difference between 2 other variables in cakephp -
i can't head around one. want validate whether third variable on cakephp form less or equal difference between 2 other variables on form. has tackled or similar?
paul
here code show have done:
public function lessthanequalto($check, $otherfield) { $value = array_values($check); $compareto = $this->data[$this->name][$otherfield]; if (!validation::comparison($value[0], 'lessorequal', $compareto)) { return false; } else { return true; } } public function numdifference($startnumber, $usednumber) { $sn = int($this->data[$this->name][$startnumber]); $un = int($this->data[$this->name][$usednumber]); return ($sn - $un); } model contains validation: second rule plain wrong, have tried number of things, coming rubbish:
public $validate = array( 'ag1_compl_dist_num'=>array( 'rule' => array('lessthanequalto','ag1_compl_start_number'), 'message' => 'value must less starting number', 'allowempty' => true, 'required' => false ), 'ag1_compl_remain' => array( 'rule' => array('lessthanequalto','numdifference'), 'message' => 'value must less difference between numbers', 'allowempty' => true, 'required' => false ) ); clearly, call numdifference should have identifies 2 numbers work out difference between..
when do:
'rule' => array('lessthanequalto','numdifference') and in lessthanequalto function $compareto = $this->data[$this->name][$otherfield];, numdifference field in data object, not exist.
one way create lessthanequaltodiff method this:
public function lessthanequaltodiff ($check, $startfield, $usedfield) { $value = array_values($check); $sn = intval($this->data[$this->name][$startfield]); $un = intval($this->data[$this->name][$usedfield]); return validation::comparison($value[0], 'lessorequal', $sn - $un) ; } then rule:
public $validate = array( 'ag1_compl_remain' => array( 'rule' => array('lessthanequaltodiff', 'ag1_compl_start_number', 'ag1_compl_used_number'), 'message' => 'value must less difference between numbers', 'allowempty' => true, 'required' => false ) );
Comments
Post a Comment