简体   繁体   中英

How to display an error form with cakePHP?

I want to know if there is a way to show an error form with cakePHP but in another view that it is not default.

This is my default View where I add data to my DB:

Default view for add data

But I have another view where I use the same form just with a different style:

My other view where I can add the same data too

I have this validation in my Model:

var $validate = array(
    'cantidad' => array(
            'rule' => 'notEmpty',
            'message' => 'Rellena este campo'),
    'articulo_id' => array(
            'rule' => array('checkArticulo', 1),
            'on' => 'create',
            'message' => 'El artículo seleccionado ya fue agregado, intenta con otro.'
        )
);      

function checkArticulo($data, $limit){
    $now = new DateTime();
    $articulo_agregado = $this->find( 'count', array('conditions' => array('AperturaArticulo.articulo_id' => $data, 'AperturaArticulo.created >=' => $now->format('Y-m-d 00:00:00'))));
    return $articulo_agregado < $limit;
} 

My field 'cantidad' must not be empty and my field 'articulo_id' must not be twice by day.

If you see in the second picture I have added already the article called 'TORTILLA 900GR'. If I try to add the same article in 'My other view where I can add the same data too' I get my validation successfully but the error is displayed in the 'Default view for add data' .

I just want to display that error in my 'My other view where I can add the same data too'

thank u!

You mean Validation with custom form / without formhelper. We can do these validations in the controller. Took the code sample from Validating Data from the Controller .

The method $this->< MODEL >->validates() in your Controller will only return true or false. To manually work on the validation errors, you'll need this:

 if ($this-><MODEL>->validates()) { // it validated logic } else { // didn't validate logic $errors = $this-><MODEL>->validationErrors; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM