Tabular Input - optional models

I am following the guide here:

http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html

I have something similar to this:


    public function actionCreate()

    {

        $settings = [new Setting(), new Setting(), new Setting(), new Setting()];


        if (Setting::loadMultiple($settings, Yii::$app->request->post()) && Setting::validateMultiple($settings)) {

            foreach ($settings as $setting) {

                // If setting is not empty then

                $setting->save(false);

            }

            return $this->redirect('index');

        }


        return $this->render('create', ['settings' => $settings]);

    }

What I am looking for is the ability to mark the whole setting model optional, so the user can skip it if they want but if they enter anything then all the validation will be performed.

The idea being that I give the user 4 lines of settings, they can just fill in two of them if they require and skip the rest.

Is there a way of doing this or should I be filtering the the post variables of empty rows before validating?

Thanks

Ed

OK, I sorted this with scenarios in the end.


$settings = [new Setting(),

             new Setting(['scenario' => 'optional']),

             new Setting(['scenario' => 'optional']),

             new Setting(['scenario' => 'optional'])];

I then just disable all the "required" rules on that scenario, in this case one Setting is required, the rest can be left blank.

Then I just check whether the Setting is blank before issuing the save, works for me…

Cheers

Ed