Override Rules

Hi, I got few form fields which are mandatory to be selected by a user, so have added rules ‘required’ for the specific fields.

But these fields are not there in db table. so while inserting into db, how do i remove these mandatory rules.

Thank You.

Hey, you should use scenarios for this:

Wiki: Understanding Scenarios

Don’t put the DB fields in a scenario, only the one’s you want to validate off the form, then validate against the scenario and save the DB fields with no validation (since you need to validate and save separately now) or switch scenarios.

Hi,

Thanks for the reply,

I gotta a db table, user_department

user_id, department_id

In my form,

i got fields,

department_id_1

department_id_2

department_id_3

my rules for user_department model

array(‘user_id, department_id’, ‘required’, ‘on’ => ‘formSubmit’),

array(‘department_id_1, department_id_2, department_id_3’, ‘required’),

its inserting into user table, but not into user_department table.

in controller

for ($i = 1; $i < 3; $i++) {

$modelUserDepartment = new UserDepartment;

$modelUserDepartment->scenario = ‘formSubmit’;

$modelUserDepartment->user_id = $user_d;

$modelUserDepartment->department_id = $POST[‘Userdepartment’]['department_id’ . $i];

}

its not dislpaying any error on submitting the form. but not inserting into db to…

Hi Dude,

Thanks a ton… It worked.

I had to do this, :)

$modelUserDepartment->setScenario(‘formSubmit’);

Hi,

you are not saving the models to the DB, you need to call [font="Courier New"]$modelUserDepartment->save()[/font] at the end of the loop.

Suggestion:

As far as I understand from your code, the form only contains the input fields for the 3 [font="Courier New"]department_id_x[/font] properties. In this case there is no need to add these non-model properties to the [font="Courier New"]UserDepartment[/font] model, instead I would create a [font="Courier New"]UserDepartmentForm[/font] model as a subclass of [font="Courier New"]CFormModel[/font] which only contains these 3 properties, and use it for handling the form. Then you can remove those fields from the [font="Courier New"]UserDepartment[/font] model.

Hi Haykel,

Yeah. i added the save() to the end the loop.

Thanks for the suggestion… Will implement it in the way you suggested.

:)