0

I have some problem when using activeform type file. When i try to send my input in registration controller the validation from model always return it false because there is input type file that empty. After i checking from html inspect elements, it turns out that the form I created on the register page generates a new input on it with the type hidden and the same name. I still can't understand why it can generate another input type hidden and input post in controller read from that input type hidden

this is my activeform:

<?php $form = ActiveForm::begin(['id' => 'form-signup', 'action' => '/site/register/userregister', 'options' => ['enctype' => 'multipart/form-data'], 'method' => 'post' ]); ?>
    
      <?= $form->field($model,'email')->textInput()->input('email', ['placeholder' => "Email"])->label(false) ?>

      <?= $form->field($model,'photofile',
              ['options' => ['class' => 'form-group']])
            ->fileInput([])
            ->label('Photo file<br/><span style="font-size:12px;font-style:italic;">(Max. size 1 MB, Allowed type: .jpg, .jpeg, .png)</span>') ?>

      <?= $form->field($model,'resumefile',
              ['options' => ['class' => 'form-group']])
            ->fileInput([])
            ->label('Resume file<br/><span style="font-size:12px;font-style:italic;">(Max. size 1 MB, Allowed type: .doc, .docx, .pdf)') ?>

      <div class="form-group">
          <?= Html::submitButton('Sign Up', ['class' => 'btn btn-sign', 'type' => 'submit']) ?>
      </div>
      <h6 class="mt-3">Have an account?&nbsp;&nbsp;<a class="font-weight-bold" href="<?= Url::to(['login/userlogin']) ?>">Sign In</a></h6>

    <?php ActiveForm::end(); ?>

this my controller :

public function actionUserregister()
  {
    $model = new SignupModel();

    if (Yii::$app->request->isPost) {

        $input = Yii::$app->request->post('SignupModel');
        echo "<pre>";var_dump($input);exit;
    }
  }

this is function in to validate rule input:

public function rules()
    {
        return [

          ['photofile', 'required'],
          ['photofile', 'file', 'extensions' => 'jpg,jpeg,png', 'maxSize' => 1024 * 1024 * 1],

          ['resumefile', 'required'],
          ['resumefile', 'file', 'extensions' => 'pdf,doc,docx', 'maxSize' => 1024 * 1024 * 1 ],
        ];
    }

and this is what i see in inspect elements:

<div class="form-group field-signupmodel-photofile required">
  <label class="control-label" for="signupmodel-photofile">Photo file</label>
  <input type="hidden" name="SignupModel[photofile]" value>    //here is the problem
  <input type="file" id="signupmodel-photofile" name="SignupModel[photofile]" aria-required="true">
  <p class="help-block help-block-error"></p>
</div>
<div class="form-group field-signupmodel-resumefile required">
  <label class="control-label" for="signupmodel-resumefile">Resume file</label>
  <input type="hidden" name="SignupModel[resumefile]" value>   //here is the problem
  <input type="file" id="signupmodel-resumefile" name="SignupModel[resumefile]" aria-required="true">
  <p class="help-block help-block-error"></p>
</div>

what's wrong with my code above?

chiper4
  • 303
  • 5
  • 18
  • 2
    The hidden input is there for a reason and should not be the cause of your problem, have a look at [this discussion](https://forum.yiiframework.com/t/file-input-hidden-field/72077) if you want to know more. There are also similar questions to yours already in SO, for example [this one](https://stackoverflow.com/a/34593768/2557030). Not sure if it's an exact duplicate though, but it gives you somewhere to start. – Raul Sauco Dec 04 '20 at 06:33
  • 2
    To be clear, you say that when you submit the form that you show above, with **both file inputs having a selected image**, you get a validation error? The variable `$input` that you are dumping has the contents of `$_POST` so it is expected that `photofile` and `resumefile` are empty, as they should be. If you want to check if the files were uploaded correctly, you need to check `$_FILES`. Easier to do in the debugger than with `var_dumps`. If you want to use the uploaded files, the recommended way is to use [`UploadedFile`](https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload) – Raul Sauco Dec 04 '20 at 06:40

0 Answers0