I’m trying to make a form to edit some fields, one of which is a file input:
<?= $form->field($model, 'image')->fileInput() ?>
At this point the user has already uploaded an image, and doesn’t need to change the original one unless s/he wants to.
Problem is, I can’t seem to find a way to prefill the fileInput()
field with the image the user first uploaded.
My controller has:
if ($model->load(Yii::$app->request->post()))
{
$model->image = UploadedFile::getInstance($model, 'image');
if ($model->editPointImage()) {
...
}
}
else
{
$get = [Table]::findOne($id);
...
$model->image = $get->image;
...
}
I looked up several threads (Form with FileInput and update action, Fileinput() Defaults To 'no File Chosen') and tried the following:
...
<?php
if ($action == 'edit')
{
echo "<img src = $model->image />";
}
?>
<?= $form->field($model, 'image')->fileInput() ?>
...
as well as
<?= $form->field($model, 'image')->fileInput(['value' => $path]) ?>
but to no avail.
I have checked the console CTRL+Shift+I in Chrome and the value for the input is correct; the form still isn’t working.
I also tried to remove the required
constraint from the image
field, but am still asked to upload a file when submitting the form without one.
Does anyone know of a solution? Thanks!