【问题标题】:CakePHP 2.0.3 Model validate data issueCakePHP 2.0.3 模型验证数据问题
【发布时间】:2011-12-15 14:51:13
【问题描述】:

好的,我有一些验证问题,因为我在表单中使用了其他名称,然后它们在表中作为示例:

<?=$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',

            );


    }
?>

【问题讨论】:

    标签: validation cakephp-2.0 cakephp-model


    【解决方案1】:

    好的,我找到了解决方案。因为模块会验证控制器中设置的数据,并使用设置的数据而不是表单中设置的值进行输出。

    因此输入输出消息的Formhelper没有任何作用。因此你必须定义它。

    像这样:

    <?
    if ($this->Form->isFieldError('prefix_price')) {
        echo $this->Form->error('prefix_price');
    }
    ?>
    <?=$this->Form->input('Product.price', array('label'=>'Preis von')) ?>
    

    然后您收到了放入模型中以进行验证的消息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多