[Bug] Validate upload_multiple field as sometimes and required
See original GitHub issueBug 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:
- Created 2 years ago
- Comments:6 (3 by maintainers)

Top Related StackOverflow Question
@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.
I think I am seeing the same issue arise with the
browse_multiplefield, 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?