CakePHP 2.0.3模型验证数据问题

发布于 2024-12-21 04:34:39 字数 5025 浏览 3 评论 0原文

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

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

            );


    }
?>

ok i have some validate issue because i am using in my Form other names then they are in the Table as example:

<?=$this->Form->input('name') ?>

and in my database table i have something like this named xyc_name

every time before i save my data i set them to let know the model what it schould do with the request data but the validation may do not know where to give the message back or something.

what should i do to put out the validated error message?

this ar my codes:

View:

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

Controller:

<?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);

}
?>

Model:

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

            );


    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮色兮凉城 2024-12-28 04:34:39

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

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

像这样:

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

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

ok i found a solution. because the modul validate the seted data in the controller and also do output with the data which was setted and not the values which was setted in the form.

And therfore the Formhelper of input to put message out have no effect. Therfore you have to define it.

like this:

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

then you got the message which you put in your model for validating

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文