Skip to content

validateAndCreate does not work with custom creation method #1058

@Khartir

Description

@Khartir

✏️ Describe the bug
According to the documentation, validation happens before the object is created. However, when using a custom creaton method (fromX()), validation is not called, when using validateAndCreate.

↪️ To Reproduce
Provide us a pest test like this one which shows the problem:

it('cannot validate data with validateAndCreate and custom creation method', function () {
    class BaseData extends Data
    {
        public function __construct(
            public string $name,
        ) {
        }
        public function fromDummy(array $payload): static
        {
            return self::from($payload);
         }
    }

    // Validation exception is not thrown, instead ArgumentCountError: Too few arguments
    dd(BaseData::validateAndCreate([]));
});

✅ Expected behavior
Validation Exception is thrown

🖥️ Versions

Laravel Data: 4.15
PHP: 8.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions