How to use multiple validation rule sets per model in cakephp

As default we have single var $validate rule set in the model class which is triggered on each save() method call while saving form data. This is quite logical and sufficient for single form fields to validate but what if we have another form with somewhat different fields which we want to post and validate? As an answer i know of three different approaches.

Approach One

As per the latest release (not sure about previous releases) cakephp treats model validations “if and only if” way. In other words, a field will only be validated if it is present in the $this->data data set and not flaged as ‘require’=>true in the var $validate rule set. This approach may come handy when there is small number of forms (1-2) and also small number of fields for each form.

I will try to explain it using examples. Let us assume we have a registration process broken down in two steps. Below is the step one form view.

And step two is:

here is the model validation rule set:

and finally controller functions. Registration Step 1 submits data to Register1 function which looks like this :

and Register2 like this one.

A simple and straight forward approach! But, someone with a habit of keeping code neat and clean may not like this way as it would mix up the validation rules for all forms and also it may become quite messed up with the increasing number of forms and/or form fields. So we need to start thinking about a different way which would provide a somewhat independent and separate validation rules. I myself adopted this approach (two) until i came across the third one (explained later).

Approach Two

In one of my cakephp projects i had to break a large form into two different forms and then to save them to a single model. My form views remained same as exampled above but what i did, i created a dummy model “Register” with $useTable=”users”. Now i had two models one User and other Register with the following sampled lines of code respectively.

and

Hold on! As I get form data in $this->data[‘User’] format i need to hack data inside my controller’s Register2 function so i could submit that data to $this->Register model object.

Here is the alteration in Register2:

And it worked. I know it was a cheap hack but i didn’t have much time to think about the third one (behaviour thing) and my forms were so big that i could not use the first one, it worked just fine for me.

Approach Three

Use of MultivalidatableBehavior by Dardo Sordi at bakery.cakephp.org seems perfect for all model validation needs. One can set different validation rule sets in a single model class and call whichever necessary. By placing the main logic in behaviour makes it portable and accessible to every model class.

Ok. Now we will see it in the context of our broken registration process. Here is the example source code of our User model class:

And here are controller functions:

and Register2:

Isn’t a more precise and clean approach? Furthermore, the method setValidation() also accepts as parameter an array with the ruleset:

And finally here is the source code of behaviour class

And thats all for multiple validation rule sets for a single model! You certainly are very patient to be with me its being a long post! Thank You.

5 thoughts on “How to use multiple validation rule sets per model in cakephp

  1. I know this is a response on an old post but…

    You know this rule isn’t a good solution?

    ‘firstname’ => array(‘rule’ => ‘/[A-Za-z ]+/’, ‘message’ => ‘Firstname must contain letters and spaces only.’),

    It wont allow input like ‘84165’ but it will allow this: ‘jelle842’.

Leave a Reply

Your email address will not be published. Required fields are marked *