最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - spatielaraval-data WithCast validation error - Stack Overflow

programmeradmin1浏览0评论

Trying to cast $address_val to Address object:

class AlbumData extends Data
{
    public string $title;
    #[WithCast(AddressCast::class)]
    public ?Address $address_val;
    
}

try {
    AlbumData::validateAndCreate([
        'title' => 'Never Gonna Give You Up',
        'address_val' => 'Never Gonna Give You Up',
    ]);
} catch (ValidationException  $exception) {
    dump($exception->validator->errors()->all());
    dd($exception->getMessage());
}

Error message:

> "The address_val field must be an array."

class Address extends Data
{
    public function __construct(
        public ?string $text,
    ) {}
}

class AddressCast implements Cast
{
    public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): Address
    {
        return new Address($value);
    }
}

What am I doing wrong?

Notes:

  1. If you use the from method instead of validateAndCreate, then there is no error and everything works properly.
  2. If Address NOT extends Data, aslo works properly.

Trying to cast $address_val to Address object:

class AlbumData extends Data
{
    public string $title;
    #[WithCast(AddressCast::class)]
    public ?Address $address_val;
    
}

try {
    AlbumData::validateAndCreate([
        'title' => 'Never Gonna Give You Up',
        'address_val' => 'Never Gonna Give You Up',
    ]);
} catch (ValidationException  $exception) {
    dump($exception->validator->errors()->all());
    dd($exception->getMessage());
}

Error message:

> "The address_val field must be an array."

class Address extends Data
{
    public function __construct(
        public ?string $text,
    ) {}
}

class AddressCast implements Cast
{
    public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): Address
    {
        return new Address($value);
    }
}

What am I doing wrong?

Notes:

  1. If you use the from method instead of validateAndCreate, then there is no error and everything works properly.
  2. If Address NOT extends Data, aslo works properly.
Share Improve this question asked Mar 12 at 20:33 EnikiBenikiEnikiBeniki 9911 gold badge13 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
class AlbumData extends Data
{
    public string $title;
    #[WithCast(AddressCast::class)]
    public ?Address $address_val;
    
}

try {
    AlbumData::validateAndCreate([
        'title' => 'Never Gonna Give You Up',
        'address_val' => 'Never Gonna Give You Up', <-- HERE
    ]);
} catch (ValidationException  $exception) {
    dump($exception->validator->errors()->all());
    dd($exception->getMessage());
}

Data class from() method doesn't perform validation and validateAndCreate() expects array like this:

AlbumData::validateAndCreate([
        'title' => 'Never Gonna Give You Up',
        'address_val' => ['text' => Never Gonna Give You Up'], <-- FIX
    ]);

Other depends on your business logic, wether to keep your class extended or not

发布评论

评论列表(0)

  1. 暂无评论