Validation scenario...


Has anyone managed to have different validation scenarios depending on one of the form fields?

For example, for a classified ads site, the ad can have 2 types : either "sell a car" or "search for a car"

from there, if the user is selling a car, horsepower and mileage need to be mandatory, whereas if the user has selected search, he doesn’t need to be so specific.

Do you think a better approach would be to have 1 form for each type or is there a way to do this in one single form?

Thanks a lot!

When I needed something like that, I used different forms.

But once I saw a similar question here in the forum and IIRC, someone managed to use only one. Maybe you can find it. Let me check the forum too if I can find that post.

Write a custom validation rule, using a function.

Take a look here for an example.

That’s a nice approach. I hope that it helps Benn :)

I’ll look into it and keep you posted! Thanks for your prompt answers :)


Thanks for the tips, however, the problem is there are no * beside the fields that are only required when the first field (a dropdown in my case) has a given value…

Mentel : have you found the post you were refering to? :)

When you were using different forms, how did you deal with validation rules? They are specified at Model level.



No, I didn’t. Sorry for the delay.

I validated like this:

$valid = $model1->validate();

$valid = $model2->validate() && $valid;

if ($valid) {




You may want to use a transaction.