Validation: Custom attributes names of DataCollections not working #369
Replies: 5 comments 6 replies
-
Could you please provide me a pest test like this: it('tt', function (){
class RelationData extends Data
{
public function __construct(
public string $name,
#[DataCollectionOf(AddressData::class)]
public DataCollection $addresses
)
{
}
public static function attributes(...$args): array
{
return [
'name' => 'naam'
];
}
}
class AddressData extends Data
{
public ?int $id;
public ?string $code;
public ?string $name;
public string $street;
public string $number;
public string $city;
#[DataCollectionOf(ContactData::class)]
public DataCollection $contacts;
public static function attributes(...$args): array
{
return [
'code' => 'code',
'street' => 'straat',
'number' => 'huisnummer',
'zipcode' => 'postcode',
'city' => 'plaats'
];
}
}
}); With the whole example, including |
Beta Was this translation helpful? Give feedback.
-
Hi @rubenvanassche, This is the first Pest test I wrote. Might not be what you expected but it does the test it seems.
|
Beta Was this translation helpful? Give feedback.
-
I think I found a related issue in livewire, it has to with collections. |
Beta Was this translation helpful? Give feedback.
-
I think you should contact the people at Laravel, now I remember they're not supporting this. Even have a disabled test for it: laravel-data/tests/ValidationTest.php Line 1823 in 9465cb8 |
Beta Was this translation helpful? Give feedback.
-
For laravel validation, this method is not static, in this case, we can do something like that: class NestRequest extends FormRequest
{
public function rules(): array
{
return [
'addresses.*.code' => 'code',
'addresses.*.street' => 'straat',
'addresses.*.number' => 'huisnummer',
'addresses.*.zipcode' => 'postcode',
'addresses.*.city' => 'plaats',
];
}
public function attributes()
{
$addresses = $this->attributes->get('addresses');
$niceNames = [];
foreach ($addresses as $idx => $address) {
foreach ($address as $key => $val) {
$rowIdx = $idx + 1;
$niceNames["addresses.{$idx}.{$key}"] = "No.{$rowIdx}, {$key}";
}
}
return $niceNames;
}
} How can we do something simillar in laravel data? |
Beta Was this translation helpful? Give feedback.
-
Consider this data class:
It has a DataCollection of AddressData:
The validation error returned for address.2.city is:
Het addresses.2.city veld is verplicht.
and notHet plaats veld is verplicht.
Also Address has a DataCollection of ContactData that also doens't work.
What I found was that ContactData attributes where not even added. Because of this line:
laravel-data/src/Resolvers/DataValidationMessagesAndAttributesResolver.php
Line 37 in 5b5b659
Adding:
!$dataProperty->type->isDataCollectable
makes sure the attributes are added.But they are still not resolved.
I stranded to find out where the resolving took place.
Anyone that had the same issue?
Beta Was this translation helpful? Give feedback.
All reactions