Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequiredWith not working as expected #853

Open
scippio opened this issue Aug 23, 2024 · 5 comments
Open

RequiredWith not working as expected #853

scippio opened this issue Aug 23, 2024 · 5 comments

Comments

@scippio
Copy link

scippio commented Aug 23, 2024

✏️ Describe the bug
I think there is a bad behaviour with RequiredWith attribute.

↪️ To Reproduce¨

it('cannot validate MyData', function () {
    class MyData extends Data
    {
        public function __construct(
            public string $name,
            #[Unique(User::class, 'email')]
            public string $email,
            #[RequiredWith('street')]
            public string|Optional $city,
            #[RequiredWith('city')]
            public string|Optional $street
        ) {
        }
    }

    // Validation exception is not thrown (off course it is but for documentation purposes it is not)
    dd(MyData::validateAndCreate([
           'name' => 'Ruben',
           'email' => '[email protected]',
           'street' => 'MyStreet'
     ]));
});

✅ Expected behavior
I think there should be validate error like: "The city field is required when street is present.", but the request is accepted. The "rules" function with "required_with" works as expected.

    public static function rules(): array
    {
        return [
            'email' => ...,
            'city' => ['required_with:street'],
            'street' => ['required_with:city'],
        ];
    }

🖥️ Versions

Laravel: 11
Laravel Data: 4.6.0
PHP: 8.3.6

@scippio
Copy link
Author

scippio commented Aug 30, 2024

I think it's same as #844

@bryanlopezinc
Copy link

@scippio i feel the combination of the sometimes (in this case Optional) and required_with rules creates a conflict.

@scippio
Copy link
Author

scippio commented Sep 19, 2024

Yes.

@bryanlopezinc
Copy link

@scippio What I meant to say is your data class validation doesn't work because of the Optional type for both properties which translates to "sometimes". notice how the rules method work.

@scippio
Copy link
Author

scippio commented Sep 19, 2024

So it's a laravel-data bug? Because Laravel works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants