Skip to content

Fix invalidMessage in Range constraint #35998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

przemyslaw-bogusz
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? no
New feature? no
Deprecations? no
License MIT
Doc PR TODO

Since Range constraint works with numbers and Datetime objects shouldn't the message that is shown if the underlying value is invalid be different?

@xabbuh xabbuh added this to the 3.4 milestone Mar 9, 2020
@@ -34,7 +34,7 @@ class Range extends Constraint

public $minMessage = 'This value should be {{ limit }} or more.';
public $maxMessage = 'This value should be {{ limit }} or less.';
public $invalidMessage = 'This value should be a valid number.';
public $invalidMessage = 'This value should be a valid number or a valid datetime.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the message here is a BC break (the fact that you needed to update the existing translation messages indicates it). We need to update the translation in the validators.en.xlf file instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh As I understand, all translations can be modified, as long as only the target field is modified and not the source field?

@@ -128,7 +128,7 @@
</trans-unit>
<trans-unit id="35">
<source>This value should be a valid number.</source>
<target>This value should be a valid number.</target>
<target>This value should be a valid number or a valid datetime.</target>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is displayed in the final UI,isn't it?
in the UI context, it might be strange to display this "or" - in a field where only a number has meaning.
Is this valid concern? Can we do something about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a second invalid message and refactor the RangeValidator a little, so that at least you don't get the message This value should be a valid number., when it's clear that you're validating datetimes, e.g. in the following scenario:

$this->validator->validate('foo', new Range(['min' => '2020-01-01', 'max' => '2020-01-31']));

While we're at it, we could also add two more messages, so that when datetime exceeds limit, you would get an earlier / later info instead of more / less.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding dedicated messages is a better idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, adding new message means, that I should switch to master. If so, I will close this PR and I will prepare a separate one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a new message in 3.4 as it fixes a display bug.

@nicolas-grekas
Copy link
Member

Up to follow up @przemyslaw-bogusz?

@przemyslaw-bogusz
Copy link
Contributor Author

Up to follow up @przemyslaw-bogusz?

Yes, I will do it. Thanks for the reminder

@przemyslaw-bogusz
Copy link
Contributor Author

I've created a new PR (#36326) for the new approach to this issue.

@przemyslaw-bogusz przemyslaw-bogusz deleted the fix_range branch April 2, 2020 22:33
fabpot added a commit that referenced this pull request Sep 24, 2020
…dator (przemyslaw-bogusz)

This PR was squashed before being merged into the 5.2-dev branch.

Discussion
----------

[Validator] Add invalid datetime message in Range validator

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

When the validated value is invalid (it isn't a number nor a datetime), but `min` or `max` option indicate that the `Range` constraint is being used to validate a datetime, the displayed message will be `This value should be a valid datetime` instead of `This value should be a valid number`. This PR replaces #35998.

Commits
-------

c777306 [Validator] Add invalid datetime message in Range validator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants