How do you perform Form validation without a model in Cakephp?

cakephp

Solution

Honestly, I'd create a model just for the validation. You can create a model that doesn't use a table by adding

var $useTable = false;

And then create a validation array with rules for each field you want to validate:

var $validate = array('login' => 'alphaNumeric','email' => 'email','born' => 'date');

Then, in your controller, do something like:

$this->MyModel->set($this->data);
if($this->MyModel->validates()){
    // do stuff with valid data
}

If you really, really can't use a model, then you'll have to simply loop over each value in `$this->data` in your controller action and validate it against a regular expression or use the `Validation::[rule]()` stuff, like:

if(Validation::email($someThingThatMightBeAnEmailAddress)){
    // do stuff with valid email address.
}

Problem

I need to perform some validation. I don't have the model in the application. Does anyone know how to do the validation without a model? Can you show me using a small sample or statement?

Original source

Related problems