Laravel-Excel icon indicating copy to clipboard operation
Laravel-Excel copied to clipboard

[Bug]: `required_without` and `required_without_all` validation not working properly.

Open YouneselBarnoussi opened this issue 1 year ago • 3 comments

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',
],

YouneselBarnoussi avatar May 12 '24 15:05 YouneselBarnoussi

Did you try just adding it manually?

                'required_without_all:*.first_name,*.last_name',

sinnbeck avatar May 31 '24 14:05 sinnbeck

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.

YouneselBarnoussi avatar Jun 01 '24 09:06 YouneselBarnoussi

If it works for required and not for required_without_all, please add a failing unit test as PR.

patrickbrouwers avatar Jun 03 '24 09:06 patrickbrouwers

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.

stale[bot] avatar Aug 10 '24 13:08 stale[bot]

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"?

kapir avatar Aug 12 '24 06:08 kapir

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.

YouneselBarnoussi avatar Aug 12 '24 20:08 YouneselBarnoussi

@YouneselBarnoussi The test case of PR looks like just needs an extra piece of logic to handle the required_without scenario. Do you have a more detailed test case, such as importing an Excel file to check if it passes validation? like this

rust17 avatar Aug 29 '24 03:08 rust17