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.

[Bug] Validate upload_multiple field as sometimes and required

See original GitHub issue

Bug report

What I did

I’m trying to validate upload_multiple field as sometimes, only required if there aren’t any existing items :

  • if there’s items selected to be uploaded, then proceed.
  • if nothing selecting, and there’re existing items, proceed also.
  • But if there aren’t any selected items and no items existing (uploaded before), then show the required field error message.

Debugging the post request, assuming the field name is photos[] it always having the first item as null :

[
    "photos" => [
        0 => null
    ]
]

validating using sometimes|required

"photos" => "between:1,4",
"photos.*" => "sometimes|required"

What I expected to happen

it should always care about existing uploaded items in the validation, see if there’s existing items uploaded then do not show required error message.

if no existing items uploaded and the submit request has empty photos then show the required error.

What happened

if an item(s) selected, it’s proceeding and uploading them. now when i try to submit the form without selecting any items (by knowing there are existing items uploaded before), it’s showing the required field error message.

What I’ve already tried to fix it

because the photos Array always having the first item as null if nothing selected as i mentioned above.

A solution to deal with this, is to provide nullable to the validation rule :

"photos.*" => "sometimes|nullable|..."

But with nullable set to rules, the required rule will never work.

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug… is it still there?

Yes

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
PHP 7.4.13

### LARAVEL VERSION:
v8.46.0

### BACKPACK VERSION:
4.1.46

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
promatikcommented, Sep 26, 2021

@tabacitu, just my two cents, I had a similar problem 1 year ago, https://github.com/Laravel-Backpack/CRUD/pull/3278. I think maybe on 4.2 we should really think about ensuring a standard behavior for all fields ✌ including, in this case, not send unfilled fields.

0reactions
tibbsacommented, Jan 11, 2022

I think I am seeing the same issue arise with the browse_multiple field, i.e. when no changes have been made, on form submit the field value is not POSTed and therefore fails validation (or if the field is not required, all existing selections are effectively deleted).

In my case, the field is truly optional – but if files have been selected, they should remain there unless specifically removed.

@AmrSubZero - did you ever create a custom validation rule to resolve this, and if so, what did that wind up looking like?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validate a multiple upload backpack field as sometimes and ...
I'm trying to validate this field as sometimes , only required if there aren't any items : if there's items selected to be...
Read more >
redcap-faq.pdf - UT Southwestern Medical Center
The advantage of the multi-field format is that you can include unknown value codes. The disadvantages are that you may need to validate...
Read more >
Snippet: Multiple Input Validation in Laravel - YouTube
Sometimes you just don't want to use JavaScript. This technique for creating, validating and using data from multiple inputs is great for ...
Read more >
File Uploads - Django documentation
If you want to upload multiple files using one form field, set the multiple HTML attribute ... Sometimes particular views require different upload...
Read more >
Problem with multiple upload validation error messages
If so, set_message() is called to set the error message for that form field. If I allow the form to upload multiple files,...
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