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

Bug: Nested LaravelData classes with collections don't propagate custom error messages #867

Closed
RasmusGodske opened this issue Sep 18, 2024 · 1 comment

Comments

@RasmusGodske
Copy link

RasmusGodske commented Sep 18, 2024

✏️ Describe the bug
When using nested LaravelData classes with collections, custom error messages defined in deeply nested classes are not being used during validation. Instead, a generic error message is displayed in the format: The selected [path.to.field] is invalid. This occurs when validating a parent class that has multiple levels of nested data collections.

↪️ To Reproduce
Here's a pest test that demonstrates the issue:

it('does not use custom error messages from deeply nested data classes', function () {
    class ModelCData extends Data
    {
        public function __construct(
            #[Required]
            public string $fieldC
        ) {}

        public static function messages()
        {
            return [
                'fieldC.required' => 'Field C is required in Model C',
            ];
        }
    }

    class ModelBData extends Data
    {
        public function __construct(
            public string $fieldB,
            #[DataCollectionOf(ModelCData::class)]
            public array $modelCCollection
        ) {}

        public static function messages()
        {
            return [
                'fieldB.required' => 'Field B is required in Model B',
            ];
        }
    }

    class ModelAData extends Data
    {
        public function __construct(
            public string $fieldA,
            #[DataCollectionOf(ModelBData::class)]
            public array $modelBCollection
        ) {}

        public static function messages()
        {
            return [
                'fieldA.required' => 'Field A is required in Model A',
            ];
        }
    }

    $attributes = [
        'fieldA' => 'Value A',
        'modelBCollection' => [
            [
                'fieldB' => 'Value B',
                'modelCCollection' => [
                    ['fieldC' => ''] // This should trigger a validation error
                ]
            ]
        ]
    ];

    try {
        ModelAData::validate($attributes);
    } catch (ValidationException $e) {
        dd($e->errors());
    }
});

The dd() output will show that instead of using the custom message "Field C is required in Model C", it produces an error like:
"modelBCollection.0.modelCCollection.0.fieldC" => ["The selected modelBCollection.0.modelCCollection.0.fieldC is invalid."]

✅ Expected behavior
The validation should use the custom error messages from all nested classes, regardless of their depth in the structure. In this case, it should return the error message "Field C is required in Model C" for the invalid fieldC.

🖥️ Versions
Laravel: 10.37.3
Laravel Data: 4.9.0
PHP: 8.3.9

EDIT
It is worth mentioning that the validation still fails on deep nested rules. This is only a problem with the error message not being correct.

@rubenvanassche
Copy link
Member

Should be fixed with the release later today!

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