在模型中调用验证函数遇到问题后,我尝试从控制器进行验证。这很好用,除了一个值错误的字段没有显示为红色并且没有显示错误消息。
传递给视图的“validationErrors”数组如下所示:
array(
'CITY' => array(
(int) 0 => 'Bitte eine Stadt angeben'
),
'CART' => array(
(int) 0 => 'Bitte etwas eingeben'
),
'DATE' => array(
(int) 0 => 'Bitte das Datum eingeben'
),
'TIME' => array(
(int) 0 => 'Bitte die Zeit eingeben'
),
'INCOME' => array(
(int) 0 => 'Bitte das Trinkgeld in Euro angeben'
),
'DELIVERYAREA' => array(
(int) 0 => 'Postleitzahl existiert nicht!'
)
)
“DELIVERYAREA”是使用以下代码动态内置的:
//form errors formatieren für plz-validierung
$this->Post->set($this->request->data['Post']);
$this->Post->validates();
$errors = $this->Post->validationErrors + $this->Zipcode->validationErrors;
//in $errors die keys ZIPCODE und DELIVERYAREA anlegen und zipcode löschen
if ($this->request->data['Post']['B/S'] == 'S' && !isset($errors['ZIPCODE'])) {
$errors['ZIPCODE'] = $errors['zipcode'];
};
if ($this->request->data['Post']['B/S'] == 'B' && !isset($errors['DELIVERYAREA'])) {
$errors['DELIVERYAREA'] = $errors['zipcode'];
};
unset($errors['zipcode']);
debug($errors);
$this->set('validationErrors', $errors);
问题是,除了 DELIVERYAREA 之外,所有字段都显示为红色,并带有特定的消息 - 我觉得很奇怪......提前感谢您的帮助