好吧我有一些验证问题因为我在我的表单中使用其他名称然后它们在表中作为示例:
<?=$this->Form->input('name') ?>
在我的数据库表中我有一个名为xyc_name
每次我保存数据之前,我都会设置它们以便让我们知道模型对请求数据的处理方式,但验证可能不知道将消息放回去的位置。
我应该怎么做才能推出经过验证的错误消息?
这是我的代码:
查看:
<?=$this->Session->flash(); ?>
<?
$subkat = '';
foreach($catList as $cat) {
if($cat['Category']['xyc_parent']!=0) {
$subkat = '-';
}else{
$subkat = '';
}
$options[$cat['Category']['xyc_id']] = $subkat.$cat['Category']['xyc_name'];
}
?>
<?=$this->Form->create('Product', array('type'=>'file')) ?>
<?=$this->Form->input('category_id', array(
'label'=>'Kategorie',
'options' => array($options)
)); ?>
<?=$this->Form->input('payment', array(
'type'=>'radio',
'options' => array(1=>'PayPal',2=>'Überweisung',3=>'Barzahlung',4=>'Abholung')
)); ?>
<?=$this->Form->input('Product.name', array('label'=>'Produktname')); ?>
<label for="descr">Beschreibung</label><?=$this->Form->textarea('Product.description', array('id'=>'descr')); ?>
<?=$this->Form->input('Product.price', array('label'=>'Preis von')) ?>
<?=$this->Form->input('Product.price_2', array('label'=>'bis')) ?>
<?=$this->Form->input('Product.quantity', array('label'=>'Anzahl der Artikel')) ?>
<?=$this->Form->input('Product.quantity_price', array('label'=>'Gesamtmengenpreis von')) ?>
<?=$this->Form->input('Product.quantity_price_2', array('label'=>'bis')) ?>
<?=$this->Form->input('Product.uvp', array('label'=>'UVP')) ?>
<?=$this->Form->input('Product.shipment',array('label'=>'Versandart')); ?>
<?=$this->Form->input('Product.shipmentprice',array('label'=>'Versandpreis')); ?>
<?=$this->Form->input('Product.articelcondition',array('label'=>'Artikelzustand')); ?>
<?=$this->Form->end(__('Senden')); ?>
控制器:
<?php
$this->Product->create();
$this->Product->set(array(
'fatcms_category_id' => $this->request->data['Product']['category_id'],
$this->userPrefix.'user_id' => $this->userLoggedIn['fatcms_auth_user_id'],
$this->productPrefix.'name' => $this->request->data['Product']['name'],
$this->productPrefix.'payment' => $this->request->data['Product']['payment'],
$this->productPrefix.'description' => $this->request->data['Product']['description'],
$this->productPrefix.'price' => $this->request->data['Product']['price'],
$this->productPrefix.'price_2' => $this->request->data['Product']['price_2'],
$this->productPrefix.'quantity' => $this->request->data['Product']['quantity'],
$this->productPrefix.'quantity_price' => $this->request->data['Product']['quantity_price'],
$this->productPrefix.'quantity_price_2' => $this->request->data['Product']['quantity_price_2'],
$this->productPrefix.'shipment' => $this->request->data['Product']['shipment'],
$this->productPrefix.'shipmentprice' => $this->request->data['Product']['shipmentprice'],
$this->productPrefix.'articelcondition' => $this->request->data['Product']['articelcondition'],
$this->productPrefix.'created' => date('Y-m-d H:i:s'),
$this->productPrefix.'uvp' => $this->request->data['Product']['uvp']
));
if($this->Product->save()) {
//CakeSession::write('productid', $this->Product->id);
$this->redirect('/products/addimg/'.$this->Product->id);
}
?>
型号:
<?php
class Product extends AppModel {
public $name = 'Product';
public $useTable = 'product';
public $primaryKey = 'xyc_id';
public $hasMany = array('Productimage'=>array(
'className'=>'Productimage',
'foreignKey'=>'xyc_id'
));
public $validate = array(
'xyc_name' => array(
'rule'=>'notEmpty',
'message'=>'test'
),
'xyc__description' => 'notEmpty',
'xyc_uvp' => 'notEmpty',
'xyc_price' => 'notEmpty',
'xyc_price_2' => 'notEmpty',
'xyc_quantity_price_1' => 'notEmpty',
'xyc_quantity_price_2' => 'notEmpty',
'xyc_payment' => 'notEmpty',
'xyc_shipment' => 'notEmpty',
'xyc_shipmentprice' => 'notEmpty',
'xyc_articelcondition' => 'notEmpty',
);
}
?>
答案 0 :(得分:0)
好的,我找到了解决方案。因为modul验证了控制器中的seted数据,并且还输出了设置的数据,而不是表单中设置的值。
因此输入消息的Formhelper无效。因此,你必须定义它。
像这样:<?
if ($this->Form->isFieldError('prefix_price')) {
echo $this->Form->error('prefix_price');
}
?>
<?=$this->Form->input('Product.price', array('label'=>'Preis von')) ?>
然后你收到了你在模型中放置的用于验证的消息