Form Using 2 Models -> Skip The Validation On One

Hello,

I have one form using to different models. I want to be able to create a new Person and eventually a new User, to give this person an account. A person doens’t need to have a User related (could be just a person used for statistics or something like that). I have a checkbox in my form “Click if you want create a user” and I show the user part of the form if it’s checked.

Then in the controller I can, with the checkbox value, know if I need call performAjax() for both Person and User or only Person.

But my problem is about the client validation. If I try to submit the form without the User required field, for example, the submition will abort and i’ll get some errors like “This field is requiered”.

So, my question is how could I skip the validation of my user model if its fields are empty (checkebox unchecked, we don’t want to create a user).

Any help are welcom :)

Sorry for my bad English.

Have a good day :)

Michaël S.

read this one yii-conditional-validator

this is used in same model . if you have two ,i think you can define a temp variable in another just for validating flag (the value rely on the checkbox value) :lol:

@yiqing95

The problem is regarding the client validation

I would go without client validation, because I think ajax validation is good enough. But if you have to do the client validation, then something like this could be done:

  1. Make ready 2 scenarios for user, one is strict and the other is loose.

  2. Use the loose scenario for making form for user in order to pass the client validation.

  3. Change to the strict scenario before ajax validation and db operation.

And optionally you may write your own client validation using javascript, in which you can peek the value of the checkbox to see if the field is required or not.

Check to see what script Yii has written for the client validation in the current form. You may get some hints from it. :)

@softark

Yes you’re right, clientValidation is not necessary required. I think I’ll go for the Ajax validation only and, as you said, maybe add some javascript if I need it B)

Thanks

Have a good day!

Michaël S.