CakePHP validationErrors empty (validation = true; saveAll = false; validationErrors = null)

594 views Asked by At

Good afternoon. 2 days I have been reading different forums and similar situations regarding the problem that occurs to me, however I have not succeeded in correcting it.

This is my situation:

Order Model

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 Model

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 Controller

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 Controller

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

Order/Test/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>

And Now --- The Problem

When I press "Crear Orden" (Save) Button, and Debug this situation I Obtain this...

/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)

If I enter all correctly, the form is saved successfully. Each field in its respective table, even as a "multiple records". The problem is that I can not display error messages, because it indicates that the validation is "true"

Please, if you can help me. Thanks a lot. Regards,

0

There are 0 answers