Good afternoon.
I have a model that has a field called "percentage". All similar models cannot have a percentage of more than 100%. A check for this is performed during the check.
I need a validation error message to indicate how many percent of the βroomβ the user has left. For example, if all MyModels already have 80%, and the user tries to create a new MyModel with a percentage of 40%, the error message will say: "Your percentage is too high. Only 20% left."
The problem is that I do not know how to put a variable in a validation error message.
In MyModel.php:
public $validate = array(
'percentage' => array(
'rule' => array('confirmValidPercentage', 'percentage'),
'message' => 'foo',
'required' => true,
),
);
public function confirmValidPercentage($data) {
$percentage = floatval($data['percentage']);
$total = 0.00;
$weights = $this->find('all', array('recursive'=>-1));
foreach ($weights as $weight) {
$total += floatval($weight[$this->name]['percentage']);
}
if ($total + $percentage > 100) {
return false;
}
else {
return true;
}
}
I tried:
$this->validate['percentage']['message'] = 'You have '.(100-$total).'% remaining';
- "foo". $validation , , "". :
unset($this->validate['percentage']['message']);
, .
- , ? .