Laravel-Excel
Laravel-Excel copied to clipboard
[Bug]: `required_without` and `required_without_all` validation not working properly.
Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?
- [X] Yes, it's still reproducable
What version of Laravel Excel are you using?
3.1.55
What version of Laravel are you using?
10.48.10
What version of PHP are you using?
8.2.16
Describe your issue
I am doing a user import as follows
class UsersImport implements ToModel, WithValidation
{
public function rules(): array
{
return [
'email' => [
'nullable',
'email',
'required_without_all:first_name,last_name',
],
'first_name' => [
'nullable',
'required_without:email',
],
'last_name' => [
'nullable',
'required_without:email',
],
];
}
}
The RowValidator class formatRules function doesn't properly format these rules as this is the output from that function
'email' => [
'nullable',
'email',
'required_without_all:*.first_name,last_name',
],
'first_name' => [
'nullable',
'required_without:email',
],
'last_name' => [
'nullable',
'required_without:email',
],
How can the issue be reproduced?
See example above
What should be the expected behaviour?
The expected output for the rules should be:
'email' => [
'nullable',
'email',
'required_without_all:*.first_name,*.last_name',
],
'first_name' => [
'nullable',
'required_without:*.email',
],
'last_name' => [
'nullable',
'required_without:*.email',
],
Did you try just adding it manually?
'required_without_all:*.first_name,*.last_name',
Hey @sinnbeck,
That doesn't work either, what does work is what I currently have as a workaround:
'required_without_all:first_name,*.last_name',
The asterisk already gets added for required validation properties but only for the first attribute/column.
Nonetheless, it is still a bug even if your answer works, as the package does add asterisks for the other required validations.
If it works for required and not for required_without_all, please add a failing unit test as PR.
This bug report has been automatically closed because it has not had recent activity. If this is still an active bug, please comment to reopen. Thank you for your contributions.
We have also experienced this. Have not had the opportunity to commit an failing unit test. But issue should be open.
Maybe it could be tagged with a "Good first pr/issue"?
Hey all, sorry for the very late PR.
I added some tests in #4183, the test 'test_format_rule_with_required_without_all' and 'test_format_rule_with_required_without' are failing with the same use case as mentioned in the initial issue.