Skip to content

[Validator] Support date format option to format invalid message #36784

@JohJohan

Description

@JohJohan

Description
With #11673 we got support for dates in validators but we do not have any option to change the formatting of the compared_value for message when the input date is invalid and we get a validation message.

Example

Current

/**
 * @Assert\GreaterThanOrEqual("2020-01-01")
 */
private $date;

Now with an invalid field we would get a validation message like:
This value should be greater than {{ compared_value }}.
real output:
This value should be greater than Jan 1, 2020, 12:00 AM
Now i see a full date with hours and minutes that i didn't pass as value.

Proposal

/**
 * @Assert\GreaterThanOrEqual(value="2020-01-01", messageFormat="Y-m-d")
 */
private $date;

Now with an invalid field we would get a validation message with our date format like:
This value should be greater than 2020-01-01.

What we also could do is look at what the input was for example value="2020-01-01 08:00" we get something like:
This value should be greater than 2020-01-01 08:00 AM because we specified hours but this might cause backwards compatibility errors as default behavior will change.

This will apply to all comparisons where we can validate date input see #11673. Let me know what you guys think i can work on a pull request if it sounds good.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions