You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
✏️ 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
{
publicfunction__construct(
#[Required]
publicstring$fieldC
) {}
publicstaticfunctionmessages()
{
return [
'fieldC.required' => 'Field C is required in Model C',
];
}
}
class ModelBData extends Data
{
publicfunction__construct(
publicstring$fieldB,
#[DataCollectionOf(ModelCData::class)]
publicarray$modelCCollection
) {}
publicstaticfunctionmessages()
{
return [
'fieldB.required' => 'Field B is required in Model B',
];
}
}
class ModelAData extends Data
{
publicfunction__construct(
publicstring$fieldA,
#[DataCollectionOf(ModelBData::class)]
publicarray$modelBCollection
) {}
publicstaticfunctionmessages()
{
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.
✏️ 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:
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.
The text was updated successfully, but these errors were encountered: