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"