Hi,
it seems like using a form with both, file input and different required input fields, where an additional submit button for uploading is provided, can’t work?!
What I’m trying to do:
I have a file input in my form, where the user can upload an image to a newly created record, but without finally saving the record.
He can also choose a file and just click the save button, and then in one step the image gets uploaded, its filename assigned to the “image_filename” attribute and the whole form/records gets saved to the DB.
What I’ve done so far:
I tried solving it over scenarios (see code below), which switch depending on the clicked button.
It partly works, but it’s difficult if an image is already uploaded and if required fields of the rest of the form are empty.
If then the save submit button is pressed, the image is gone, the assigned “image_filename” too.
If I use a hidden field in the form for the “image_filename”,
<input type=“hidden” name=“uploaded” value="<?= $model->uploaded ?>">
<?= $form->field($model, “image_filename”)->hiddenInput()->label(false) ?>
it gets transported over state after clicking the save button without providing required fields.
But after fill out required fields and pressing save submit button again, I always land in the if-condition on line 74 in the controller, that wants to upload again (an empty image!).
I do know that with this uploaded flag, I can only upload one image if set to true.
Not sure if the whole way how I do this, is “good”.
My code:
Controller (create function)
Model
View (_form) https://pastebin.com/6LT6NM9j
I’ve already searched the web and here, where I found this similar cases:
Seems the problem is common but no real solution. Maybe most just switch to an AJAX based solution or implement an extra step/form for just uploading the file. Hm.
Do I something wrong or not grasp a concept?
How do you guys handle this or are you doing completely different things?
Thanks in advance