question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upload field type on edit required issue

See original GitHub issue

Hey, i’m fairly new to backpack i have a field in my MusicCrudController: $this->crud->addField([ // Music Upload 'name' => 'music', 'label' => 'Music', 'type' => 'upload', 'upload' => true, 'disk' => 'public', ]); everything is ok when i add a new Music using CrudController, but when i try to edit i get the following message: Please fix the following errors: The music field is required. even though the field show the address of the uploaded music Musics/9c30dc40684eaefb144fda9aca2ae414.mp3

I’m so frustrated. Can anyone help me with this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:26 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
splatEriccommented, May 2, 2018

I know this is a closed issue, but I found this after searching for a solution for a similar issue. I believe I have worked out the cause of the problem. If you have an upload field named file_upload, the javascript component in backpack will submit this as a single file type input field on create. However, when you edit the file, this field is not present unless you try to upload a new file. As a result, the Laravel validation doesn’t see a file_upload key in the request input, and throws the validation error.

The problem then comes if someone clicks the remove button when editing, as the javascript of the form element will create an empty file_upload field, which will trigger the removal of the file through the standard crud process, as this is viewed as an effort to delete the uploaded object.

So the question is then, how to have validation that checks that either the input doesn’t have a file_upload attribute (in which case we know that the Crud won’t delete the current file), or if it does that it is not empty.

Awesomely, it turns out Laravel has you covered. The validation rule just needs to be:

'file_upload' => 'sometimes|required'
2reactions
tabacitucommented, May 26, 2021

@splatEric - thanks a lot for clearing that up for me. sometimes sounds PERFECT then 😃 I’ll use it myself the next time I need this, thanks a lot for teaching me about it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Making 'file' input element mandatory (required) - Stack Overflow
You can try applying the form attribute to the input field to make sure it is related to your form. <input type="file" name=""...
Read more >
File Upload Error and missing required fields - Jotform
To edit the allowed file types, select the upload field and then click on the Properties gear. Then select the Options tab and...
Read more >
<input type="file"> - HTML: HyperText Markup Language | MDN
This string is a comma-separated list of unique file type specifiers. Because a given file type may be identified in more than one...
Read more >
Custom edit form with a required field auto check in - TechNet
1) Have at least one required field in a document library. 2) Create a custom edit form for the doc lib. 2) upload...
Read more >
A Complete Guide to the WPForms File Upload Field
To view these options, you'll need to open the form builder and click on the File Upload field in the preview area to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found