mrdaliri
mrdaliri

Reputation: 7358

validate data in an array

Can the validates method validate user-defined arrays? for example:

Model:

App::uses('AppModel', 'Model');
class Recipe extends AppModel {
    public $validate = array(
        'price' => 'numeric'
    );
}

And in Controller:

App::uses('AppController', 'Controller');
class RecipesController extends AppController {
    public function add() {
        if($this->request->is('post') && $this->request->data){
            $data = array('price' => $this->request->data['myprice']);
            $this->Reservation->validates($data); //validate the $data array
        }
        else{
            throw new NotFoundException();
        }
    }
}

Upvotes: 0

Views: 65

Answers (2)

Krishna
Krishna

Reputation: 1540

for manually validate you should try this :

$this->Reservation->set( $data);
if($this->Reservation->validates(){
 //your code
}else{ 
    $this->validateErrors($this->Reservation);
}

Upvotes: 1

HerrKuchen
HerrKuchen

Reputation: 25

in your controller you can work with a fieldlist like this:

            if ($this->Model->validates(array(
                    'fieldList' => array(
                        'reason',
                        'name',
                        'message',
                    )
                ))) {

                }

Hope thats what youre looking for.

Upvotes: 0

Related Questions