0

Im using activeform. In my User model, i initialized a model property Public formType;, set its rule to safe and i am trying to use this property with hiddeninput to create condition in the user controller. But i am getting that the activeform doesn't update the value of the property. Ive read this but i am still unclear whats the workaround of updating the property while still using activeform.

Form

<?= $form->field($model, 'formType')->hiddenInput(['value' => 'userRowUpdate'])->label(false) ?>

User Controller

    public function actionUpdate($id) {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {
            $model->scannedFile = \yii\web\UploadedFile::getInstance($model, 'scannedFile');
            $type = Yii::$app->request->post('formType');
            if ($model->processAndSave()) {
                FlashHandler::success("User profile updated success!");
                if ($type == "userDetailView") {
                    return $this->redirect(['view', 'id' => $model->id]);
                } else if ($type == "userRowUpdate") {
                    return $this->redirect(Yii::$app->request->referrer);
                }
            } else {
                FlashHandler::err("User profile updated FAIL!");
            }
        }

        return $this->render('update', [
                    'model' => $model,
        ]);
    }
yai
  • 72
  • 11
  • Why you need hidden input if it's value is predefined? I don't see the use case of hidden input from the code your provided. Still you can set `$model->formType = 'userRowUpdate'` after `$model = $this->findModel()` – Insane Skull Aug 26 '22 at 03:49
  • Ive edited the user controller to show use case. I want to sent a hiddeninput parameter to the action as a condition. Doing `$model->formType = 'userRowUpdate'` would mean i need to initialize 2 model property if following my use case above right? My current understanding is the model property is an attribute which we can pass data through POST. So does that mean the hiddeninput `value=` above is pointless because its an active field? @InsaneSkull – yai Aug 26 '22 at 06:19
  • No it's not, the way you are passing the value should work as well, have you checked the post data? – Insane Skull Aug 26 '22 at 10:01
  • ive checked the post data and its null. meaning the data didnt even get to _POST. Would there be a way still for me to pass some data without using the model? Since its only used in the actioncontroller and none other place. @InsaneSkull – yai Aug 29 '22 at 01:02

2 Answers2

0

Replace the hiddeninput from using activeform to

<?=Html::hiddenInput('name', $value);?>

Reasons:-

  • code in question is probably not the right approach since its creating a attribute/model property just for the condition in action controller
  • html input wouldnt affect the model but the data is still readable and be used in the actioncontroller as a condition
yai
  • 72
  • 11
0

You can use

Html::activeHiddenInput($model, 'formType')

to avoid hiding the label and it's shorter.

if the value is not being updated, check your rules in the model. That attribute at least must have a safe rule to be able to be assigned on the $model->load(Yii::$pp->request->post()) call