CakePHP 2.0.3 Модель проверки данных проблема - PullRequest
0 голосов
/ 15 декабря 2011

хорошо, у меня есть проблема с проверкой, потому что я использую в своей форме другие имена, тогда как они указаны в таблице, например:

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

            );


    }
?>

1 Ответ

0 голосов
/ 16 декабря 2011

хорошо, я нашел решение.потому что модуль проверяет сохраненные данные в контроллере и также выводит данные, которые были установлены, а не значения, которые были установлены в форме.

И поэтому Formhelper ввода для вывода сообщения не имеет никакого эффекта,Поэтому вы должны определить его.

следующим образом:

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

, тогда вы получили сообщение, которое вы добавили в свою модель для проверки

...