Multiple model validation (required fields)

Hi.

I get tired of this problem all day.

I have two models: "Ogloszenia" and "ORuchomosci".

I tried to use one form - adding and editing data.

Everythig works fine, but the problem is that the model of "AS BABS" have fields that are required. I do not know how to make the validation of these fields.

I need your help.

My files:

OgloszeniaController:




public function actionCreate()

    {

        $model = new Ogloszenia();

		$model2 = new ORuchomosci();

	

		

        if ($model->load(Yii::$app->request->post())) {

			

			$model->userid = \Yii::$app->user->identity->id;

			$model->dodano = date("Y-m-d");

			$model->save();

			

			if($model->typ_ogl != 3) {

				

			if($model->idk == 1) {

				$model2->load(Yii::$app->request->post());

				$model2->idogl = $model->id;

				$model2->save();

			}

			

			}

			

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('create', [

                'model' => $model,

				'model2' => $model2,

				            ]);

        }

    }


/**************************/


    public function actionUpdate($id)

    {

        $model = $this->findModel($id);

		if($model->idk == 1) {

			$model2 = $this->findRuchomosci($id);

		}

		

		if($model->idk > 3) {

			$model2 = new ORuchomosci();

		}

		

        if ($model->load(Yii::$app->request->post()) && $model->save()) {




			if($model->typ_ogl != 3) {

				

			if($model->idk == 1) $model2->load(Yii::$app->request->post() && $model2->save());

			

			}

			

            return $this->redirect(['view', 'id' => $model->id]);

        } else {


            return $this->render('update', [

                'model' => $model,

				'model2' => $model2,

            ]);

        }

    }



ORuchomosci.php:




public function rules()

    {

        return [

		[['c_marka_osobowe'], 'required', 'whenClient' => "function (attribute, value) { return $('#podkategoria').val() == 8; }"],

		[['c_marka_ciezarowe'], 'required', 'whenClient' => "function (attribute, value) { return $('#podkategoria').val() == 9; }"],

		[['c_marka_motory'], 'required', 'whenClient' => "function (attribute, value) { return $('#podkategoria').val() == 10; }"],

		[['c_maszyny'], 'required', 'whenClient' => "function (attribute, value) { return $('#podkategoria').val() == 7; }"],

        ];

    }



I tried also add:


'when' => function($model) { return $model->idpk == 6; }, 

but then I get an error: "Undefined variable"

unable to find the problem. pl try jQuery instead of $.

If Your data is not saving you can do->

$model2->save(false);

this will ignore validation errors and your data will be saved.

Hi,

Not sure if I understood your question correct,

but if you want to do display validation errors for both / multiple models at the same time you could also:




// perform validation for both models

$valid = $modelA->validate(); 

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


// both models are valid

if($valid){

  // save both models

  $modelA->save(false); 

  $modelB->save(false);

}



Regards