[SOLVED] Best way to treat fields in validation as required, which aren't 'required' in the model?

I’ve got the following constellation:

1.) User has to fill out a form --> create is initialized (concerning 3 tables)

2.) As a result, if at 1.) everything went right, the user gets a second form, where he has to fill out some further fields --> update on some of the tables of 1.)

The problem is now, that I want to use ajax validation on the fields of the second form. Especially the fields should be treated as “required” … but in the first form, one of the creates goes on the same table, so there the fields shouldn’t be required (because the user doesn’t get these fields).

Filling out the fields at 1.) with dummy values isn’t really the solution I want to take, because in the database it would be a lot cleaner to leave these fields empty, if e.g. the user cancels the operation after posting the first form.

Thanks a lot in advance,

Mayjestic

Scenarios is the key in Yii to handle these situations.




// validation array

array('username', 'required', 'on'=>'your_scenario_name'),



Just define validation rules for your first form => first scenario

For the second form you define other validations => an other scenario

Ahh thanks a lot kokomo! … and again learned something new ;-).

For everyone else who needs something similar, here’s a nice tutorial: http://php-thoughts.cubedwater.com/2009/validation-scenarios/

really that tutorial is good one for scenario base validation