Skip to content

Allow sub-path inside a constructor. #760

@eigan

Description

@eigan

I have an object with several properties, and I want to write messages for specific paths inside the object.

Example

class FooDto
{
    public function __construct(
        public \DateTimeImmutable $start,
        public \DateTimeImmutable $end,
    ) {
        if ($this->start > $this->end) {
            throw new ValidationException('Start date cannot be after end date', 'start');
        }
    }
}

I want this to return:

{
  "foo.start": "Start date cannot be after end date"
}

This was pretty easy to hack into valinor with a HasPath interface, and:

if ($this->message instanceof HasPath) {
    return $this->path . "." . $this->message->path();
}

Though it would perhaps also be nice to be able to throw a collection of messages which would be applied for the entire object..

{
  "foo.title": "Title cannot be empty when `id` is null"
  "foo.start": "Start date cannot be after end date"
}

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions