Skip to content

[Form] Improve invalid messages for form types #27142

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 5 commits into from
Closed

[Form] Improve invalid messages for form types #27142

wants to merge 5 commits into from

Conversation

hiddewie
Copy link
Contributor

@hiddewie hiddewie commented May 3, 2018

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #5946
License MIT

Improvement of #14106. Added invalid_message fields to all form types. This gives better errors to users instead of the default Invalid value error.

Backwards compatibility

After discussion about BC, the following additions have been made:

  • An framework option framework.form.improved_validation_messages (default false) for enabling improved validation messages globally.
  • A form validation extension option improved_validation_messages which enables improved validation messages for that type.
  • An added parameter for the ValidatorExtension and FormTypeValidatorExtension (default false) to enable/disable the improved validation messages for that extension.
  • All the form types now check for the enabled opt-in configuration for improved validation messages. Otherwise, the previous default is used which should be the original message.

Upgrade path

  • These changes should be backwards compatible.
  • For Symfony 5.0, the improved validation messages configuration in the framework and form should be true (enabled) by default.
  • The options can be deprecated entirely in Symfony 5.1, to be removed in Symfony 6.0.

@nicolas-grekas nicolas-grekas added this to the next milestone May 3, 2018
@nicolas-grekas nicolas-grekas changed the title Improve invalid messages for form types [Form] Improve invalid messages for form types May 4, 2018
Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

Looks good to me. We should add all these new error messages in translations as well.

@@ -58,6 +58,7 @@ public function configureOptions(OptionsResolver $resolver)
'divisor' => 1,
'currency' => 'EUR',
'compound' => false,
'invalid_message' => 'The money is invalid.',
Copy link
Member

Choose a reason for hiding this comment

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

money amount?

@hiddewie
Copy link
Contributor Author

hiddewie commented May 18, 2018

@fabpot Thank you for the review. I've added your comment for the translation for the money type. As for the translations, do you mean all the files in src\Symfony\Component\Form\Resources\translations\validators.*.xlf? Of course I can add the translation keys there, but I cannot do translations in all those different languages (except Dutch and maybe German).

@hiddewie
Copy link
Contributor Author

hiddewie commented Sep 9, 2018

@fabian I've added the translations to the English file. However, I am not sure what the correct precedure is for all the other languages (of which I can only translate 2 myself).

@xabbuh
Copy link
Member

xabbuh commented Sep 15, 2018

I am not sure that it is a good idea to change the default value for this option for all built-in form types. This means that custom translations in project wouldn't be working anymore after updating to a Symfony version containing this feature. That's not a good DX IMO.

@hiddewie
Copy link
Contributor Author

@xabbuh You are right, these changes could be seen as backwards incompatible. What do you propose: close the PR or leave it open until there is a major new version?

@fabpot
Copy link
Member

fabpot commented Oct 10, 2018

I didn't thought about the BC issue here. Looks like a deal breaker :( Let's close then. Sorry @hiddewie

@fabpot fabpot closed this Oct 10, 2018
@ogizanagi
Copy link
Contributor

Perhaps we could reopen and find a proper upgrade path?

For instance, register a better_invalid_messages or [INSERT_BETTER_NAME] option from FormTypeValidatorExtension. Then if, if better_invalid_messages is false, trigger a deprecation.
A framework.form.better_invalid_messages framework option can be used to configure the default value used by FormTypeValidatorExtension globally, so any new project can be free of deprecations.

Then, in 5.1, deprecate better_invalid_messages and framework.form.better_invalid_messages to be removed in 6.0 as it won't have any effect anymore.

This seems a lot, but I find this PR valuable. Actually, IMHO, the BC break here is sensible.

@fabpot fabpot reopened this Oct 10, 2018
@hiddewie
Copy link
Contributor Author

@ogizanagi That would be possible! Would an option name of improved_validation_messages work?

What is the proper precedure for deprecating something in a far future release? Just open a new issue after this PR is merged (if it is merged at all)?

@ogizanagi
Copy link
Contributor

What is the proper procedure for deprecating something in a far future release? Just open a new issue after this PR is merged (if it is merged at all)?

Actually I don't think we have any specific workflow for this.
An issue could be great, but can be easily forgotten.
We could open a note on a project in https://github.com/symfony/symfony/projects in order to not forget this, but this kind of situation doesn't happen really often.
But I'm confident anyone doing the PR to remove the BC layer in 5.0 will think about doing one to deprecate it in 5.1 :)

@hiddewie
Copy link
Contributor Author

hiddewie commented Oct 21, 2018

@ogizanagi I've made some changes. Review is very welcome. The PR has grown considerably in code and also in complexity, but I hope everything works out.

Also see the updated description of the PR for some notes on the upgrade path. Mostly what you described.

I've enabled the improved messages in most of the tests, to avoid breaking the tests because of the deprecation message. Marking them all as legacy does not seem as a good idea to me. I've made some new tests that check the backwards deprecation notice both for framework and form).

Copy link
Contributor

@ogizanagi ogizanagi left a comment

Choose a reason for hiding this comment

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

Great work @hiddewie
Note we could set this to true by default for new projects by adding a form recipe.

@hiddewie
Copy link
Contributor Author

hiddewie commented Oct 22, 2018

@ogizanagi I've processed your comments. Thanks for the input.

  • I will look into updating the receipe.
  • I also want to write tests for at least some form types in the Core extension and using the Type Validator Extension which check the old and the new error messages.
  • I have to update the changelog files for all these new framework/form parameters/options.

@@ -25,9 +25,12 @@
class ValidatorExtension extends AbstractExtension
{
private $validator;
private $improvedValidationMessages;
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of this integration wouldnt setting e.g. static FormType::$legacyErrorMessages = false be simpler? That's something we can get rid of easily in 5.0 (mark it internal): set it from the bundle extension and access it in each form type

Copy link
Contributor

Choose a reason for hiding this comment

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

That'd be less flexible (as it'll only be global) if you want to update part of your app step by step...but regarding the number of translations to update, that might be an option to consider.

Copy link
Contributor

Choose a reason for hiding this comment

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

introducing a new improved_validation_messages form type option, we have to deprecate again in 5.0 and remove in 6.0, is not worth it IMHO

@hiddewie
Copy link
Contributor Author

@ro0NL @ogizanagi Processed some comments.

Copy link
Contributor

@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

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

Thank you for improving this in a BC way!
It would be nice to have other reviews to be sure we cannot improve some wording more before breaking BC later. @weaverryan @javiereguiluz would you mind having a look at this one?

@hiddewie
Copy link
Contributor Author

@HeahDude I've processed your comments. Didn't think of the types which cannot be invalid.

@HeahDude
Copy link
Contributor

Thank you @hiddewie!

I've reviewed it again and it looks good to me. But we need to adjust some things now that #28731 as been merged. Because the option did not exist in core before, only added by the validator extension (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php#L59).

So here's what I spotted so far:

ping @xabbuh what do you think?

@hiddewie
Copy link
Contributor Author

@HeahDude Thanks. Processed comment.

I'm unsure if I should remove the same code from https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php#L117 (I don't think so since any forms with the constraints option are excluded from the new extension).

@fabpot
Copy link
Member

fabpot commented Jan 1, 2019

@hiddewie For this pull request to be merged, you need to rebase it (to remove the merge commits).

@hiddewie
Copy link
Contributor Author

hiddewie commented Jan 2, 2019

@fabpot Done.

Copy link
Contributor

@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

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

Thank you! another round of review :)

@hiddewie
Copy link
Contributor Author

hiddewie commented Jan 26, 2019

@HeahDude Thanks for the review. I've added a load of tests for the custom validation messages, including the legacy variant.

I also noticed that the logic for legacy / non-legacy was flipped in the form types. Has been corrected.

Also rebased for new array syntax changes.

@hiddewie
Copy link
Contributor Author

hiddewie commented Feb 4, 2019

@xabbuh Done!

@xabbuh
Copy link
Member

xabbuh commented Apr 6, 2019

Thank you very much for your pull request!

As part of the Symfony EU funded hackathon (https://symfony.com/blog/the-symfony-and-api-platform-hackathon-is-coming), we were able to assemble most of the core team and big contributors in one place. Our goal is to finish as many open issues and PRs as possible.

Your commits will not be lost and you will therefore get credit for your work. All that will happen is that your commits will be moved to a new PR (see #30931) where all remaining concerns will be addressed.

Without your work this would not have been possible. So thank you once again!

@hiddewie
Copy link
Contributor Author

hiddewie commented Apr 7, 2019

No problem, thanks for the work!

@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
xabbuh added a commit that referenced this pull request Jul 10, 2020
This PR was merged into the 5.2-dev branch.

Discussion
----------

[Form] Improve invalid messages for form types

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #27142, #5946
| License       | MIT
| Doc PR        | TODO

This merge request is a continuation of #27142.

Changes done here:

* rebased on master
* made the error messages friendlier

Commits
-------

8728927 Improve invalid messages for form types
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.

8 participants