CakePHP validationErrors为空(validation = true; saveAll = false; validationErrors = null)

时间:2014-11-26 17:53:38

标签: validation cakephp message show validationerror

下午好。 2天我一直在阅读关于我遇到的问题的不同论坛和类似情况,但是我没有成功纠正它。

这是我的情况:

订单型号

class Order extends AppModel {

    public $belongsTo = array("Client", "OrderType");
    public $hasMany = array(
        'OrderDetail' => array(
            'className' => 'OrderDetail',
            'foreignKey' => 'order_id',
            'counterCache' => true
        )
    );

    public $validate = array(
        'client_id' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Favor seleccione un Cliente'
            )
        ),
        'date_from' => array(
            'required' => array(
                'rule' => array('email', 'notEmpty'),
                'message' => 'Favor ingresar correo'
            )
        )
    );
}

OrderDetail模型

class OrderDetail extends AppModel {

    public $belongsTo = array('Product', 'Smell', 'Order');

    public $validate = array(
        'product_id' => array(
            'required' => true,
            'rule' => array('notEmpty'),
            'message' => 'Debe ingresar un Producto'
        ),
        'quantity' => array(
            'required' => true,
            'rule' => array('notEmpty'),
            'message' => 'Debe ingresar una Cantidad'
        )
    );
}

OrdersController控制器

class OrdersController extends AppController {

    public function index() {
        $this->set('title_for_layout', 'Ordenes');
        $this->layout="panel";
    }

    /* Function para Pruebas */
    public function test($mode = null) {
        $this->set('title_for_layout', 'Ordenes de Prueba');
        $this->layout="panel";

        $data = $this->Order->Client->find('list', array('fields' => array('id', 'nombre'), 'order' => array('nombre ASC')));
        $this->set('clients', $data);

        $data = $this->Order->OrderDetail->Product->find('list', array('fields' => array('id', 'name'), 'order' => array('name ASC')));
        $this->set('products', $data);

        $data = $this->Order->OrderDetail->Smell->find('list', array('fields' => array('id', 'name'), 'order' => array('name ASC')));
        $this->set('smells', $data);

        if(is_null($mode)) {
            $this -> render('Tests/index');
        }

        if($mode == 'new') {
            $this -> render('Tests/new');

            // Guardar Nueva Orden
            if ($this->request->is('post')) {

                /*
                $validates = $this->Order->validates($this->request->data);
                debug($validates);
                $saved = $this->Order->saveAll($this->request->data);
                debug($saved);
                debug($this->validationErrors);
                */

                if($this->Order->saveAll($this->request->data)) {
                    $this->Session->setFlash('Nueva Orden Creada', 'flash_success');
                    return $this->redirect('/orders/test');
                } else {
                    $this->Session->setFlash('La orden no pudo ser creada, favor revise el formulario','default',array('class' => 'alert alert-danger center'));
                    return $this->redirect('/orders/test/new');             
                }



            }
        }
    }

    /* Function para Servicio */
    public function service($mode = null) {
        $this->set('title_for_layout', 'Ordenes de Servicio');
        $this->layout="panel";

        if(is_null($mode)) {
            $this -> render('Services/index');
        }

        if($mode == 'new') {
            $this -> render('Services/new');
        }
    }

    /* Function para Retiros */
    public function removal($mode = null) {
        $this->set('title_for_layout', 'Ordenes de Retiro');
        $this->layout="panel";

        if(is_null($mode)) {
            $this -> render('Removals/index');
        }

        if($mode == 'new') {
            $this -> render('Removals/new');
        }
    }

}

OrderDetailController控制器

Do not exist (I assumed that I do not need it for this purpose)

订单/测试/ new.ctp

<div class="matter">
    <div class="container">
        <!-- Today status. jQuery Sparkline plugin used. -->


        <!-- Today status ends -->
        <div class="row">
            <div class="col-md-12">

                <div class="widget">
                    <div class="widget-head">
                        <div class="pull-left">Nueva Orden de Prueba</div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="widget-content">
                        <div class="padd">

                            <!-- Content goes here -->

                            <!-- Flash Message -->
                            <div class="form-group">
                                <div class="col-lg-12">
                                    <?php echo $this->Session->flash(); ?>
                                </div>
                            </div>

                            <?php echo $this->Form->create('Order', array('class'=>'form-horizontal', 'novalidate'=>'novalidate')); ?>
                            <h3>Datos del Cliente</h3>
                            <div class="widget">
                                <div class="widget-content">
                                    <div class="padd">

                                        <!-- Cliente y Tipo Orden -->
                                        <div class="form-group">
                                            <label class="control-label col-lg-1">Cliente *</label>
                                            <div class="col-lg-5">
                                                <?php echo $this->Form->input('Order.0.client_id', array('label'=>false, 'class'=>'form-control chosen-select', 'placeholder'=>'Seleccione Cliente', 'type'=>'select','options' => $clients, 'empty' => 'Seleccione Cliente')); ?>
                                            </div>
                                            <div class="col-lg-1">
                                                <?php echo $this->Html->link('Nuevo Cliente', '/clients/add', array('class'=>'btn btn-primary')); ?>
                                            </div>
                                            <?php echo $this->Form->input('Order.0.order_type_id', array('label'=>false, 'class'=>'form-control', 'placeholder'=>'Ingrese Tipo de Orden', 'type'=>'hidden', 'value'=>3)); ?>

                                        </div>

                                    </div>
                                </div>
                            </div>

                            <h3>Vigencia tentativa de la Prueba</h3>
                            <div class="widget">
                                <div class="widget-content">
                                    <div class="padd">

                                        <div class="form-group">
                                            <label class="control-label col-lg-1">Desde</label>
                                            <div class="col-lg-2">
                                                <?php echo $this->Form->input('Order.0.date_from', array('label'=>false, 'class'=>'form-control datepicker', 'placeholder'=>'Desde', 'type'=>'text')); ?>
                                            </div>
                                            <label class="control-label col-lg-1">Hasta</label>
                                            <div class="col-lg-2">
                                                <?php echo $this->Form->input('Order.0.date_to', array('label'=>false, 'class'=>'form-control datepicker', 'placeholder'=>'Hasta', 'type'=>'text')); ?>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-lg-12">
                                    <h3 class="pull-left">Elementos a incorporar en Prueba</h3>
                                    <button type="button" class="btn addRow pull-right btn-primary"><i class="fa fa-plus"></i> Agregar Item</button>
                                </div>
                            </div>

                            <div class="widget">
                                <div class="widget-content">

                                    <table class="table table-striped table-bordered table-hover">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Item</th>
                                                <th>Cantidad</th>
                                                <th>Aroma</th>
                                                <th>Nº Registro</th>
                                                <th width="90px;">Acciones</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php for ($i=0;$i<10;$i++) { ?>
                                            <tr>
                                                <td><?php echo $i+1; ?></td>
                                                <td><?php echo $this->Form->input('OrderDetail.'.$i.'.product_id', array('label'=>false, 'class'=>'form-control chosen-select', 'type'=>'select', 'options' => $products, 'empty' => 'Seleccione Item', 'disabled' => 'disabled')); ?></td>
                                                <td><?php echo $this->Form->input('OrderDetail.'.$i.'.quantity', array('label'=>false, 'class'=>'form-control', 'placeholder'=>'Cantidad', 'type'=>'text', 'disabled' => 'disabled')); ?></td>
                                                <td><?php echo $this->Form->input('OrderDetail.'.$i.'.smell_id', array('label'=>false, 'class'=>'form-control chosen-select', 'placeholder'=>'Aromas', 'type'=>'select', 'options' => $smells, 'empty' => 'Seleccione Aroma', 'disabled' => 'disabled')); ?></td>
                                                <td><?php echo $this->Form->input('OrderDetail.'.$i.'.product_number', array('label'=>false, 'class'=>'form-control', 'placeholder'=>'Identificador de Item', 'type'=>'text', 'disabled' => 'disabled')); ?></td>
                                                <td>
                                                    <center>
                                                        <?php if($i!=0) { ?>
                                                        <button type="button" class="btn removeRow btn-xs btn-danger"><i class="fa fa-times"></i> Borrar</button>
                                                        <?php } ?>
                                                    </center>
                                                </td>
                                            </tr>
                                            <?php } ?>                                                       
                                        </tbody>
                                    </table>

                                </div>
                            </div>

                            <!-- Content ends here -->

                        </div>
                    </div>
                    <div class="widget-foot">
                            <!-- Botones -->
                            <div class="form-group">
                                <div class="col-lg-12">
                                    <?php echo $this->Form->button('Crear Orden', array('class' => array('btn', 'btn-success', 'pull-right'), 'type'=>'submit')); ?>
                                </div>
                            </div>
                    </div>
                    <?php echo  $this->Form->end(); ?>
                </div>  

            </div>
        </div>

    </div>
</div>

现在---问题

当我按下“Crear Orden”(保存)按钮,并调试这种情况时,我获得了这个......

/app/Controller/OrdersController.php (line 36) 
true -> This is debug($this->Order->validates($this->request->data))
/app/Controller/OrdersController.php (line 38)
false -> This is debug($this->Order->saveAll($this->request->data))
/app/Controller/OrdersController.php (line 39)
null -> This is debug($this->validationErrors)

如果输入正确,表单将成功保存。各自表中的每个字段,甚至作为“多个记录”。 问题是我无法显示错误消息,因为它表明验证是“true”

如果你能帮助我,请。 非常感谢。 的问候,

0 个答案:

没有答案