Review main Merged

81696[FEATURE] Allow custom translations for extbase validators

Torben Hansen CR +2 V +2 Hard +414 −140 updated 06/07/24, 10:34

Commit message

Extbase validators use hardcoded translation keys to display messages
when a validated value is not valid. It is however only possible to
overwrite the translation keys globally, which in many scenarios is
not suitable. It is also not possible to define an individual
translation key on property or validator basis, so translated
messages can not be individualized for the current usage scope.

This feature introduces the possibility to define custom translation
keys or a custom message for extbase validators by using validator
options. This allows developers to provide individual translation
keys for the current usage scope as shown in the example below:

```
    'validator' => 'NotEmpty',
    'options' => [
        'nullMessage' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:validation.myProperty.notNull',
        'emptyMessage' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:validation.myProperty.notEmpty',
    ],
])]
protected string $title = '';
```

This change also deprecates the validator option `errorMessage`
of the `RegularExpressionValidator` in favor to the new
`message` option.

Additionally, 3 unused translation keys in the locallang.xlf
file have been removed.

Review

  • Code review CR +2 Done
    • Benni Mack +2
    • Garvin Hicking +1
    • Simon Schaufelberger +1
  • Verified V +2 Done
    • Benni Mack +2
    • Oliver Klee +1
    • Garvin Hicking +1
    • Simon Schaufelberger +1
    • core-ci +1 CI

Issues 1

Files 23

Show 3 more files

Review log 5979

Show 3854 earlier messages
  1. Torben Hansen uploaded patch set 1 11/06/23, 15:03

  2. core-ci voted on patch set 1 V -1 11/06/23, 16:03 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/48662

  3. Torben Hansen uploaded patch set 2 V -1 11/06/23, 17:45

  4. core-ci voted on patch set 2 V +1 11/06/23, 17:52 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/48666

  5. Torben Hansen updated the commit message in patch set 3 V +1 11/06/23, 18:15

  6. core-ci voted on patch set 3 V +1 11/06/23, 18:23 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/48667

  7. Torben Hansen updated the commit message in patch set 4 V +1 11/07/23, 08:29

  8. core-ci voted on patch set 4 V +1 11/07/23, 08:37 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/48677

  9. Garvin Hicking commented on patch set 4 11/07/23, 09:41

    Personally, I love the idea. We (my team) usually had to copy+paste these as our own custom validators. That is a valid ;) way to go, since Validators aren't usually that big. But it creates redundancy, and we'd rather stay closer to the core. It makes attributes possibly feel a bit "bloated". But since it's totally optional, I don't see too much harm in this right now. Curious what others say. So, I'm totally in favor of pursueing this, thanks for this initiative!
  10. Torben Hansen rebased onto patch set 5 V +1 11/10/23, 17:04

  11. core-ci voted on patch set 5 V +1 11/10/23, 17:11 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/48796

  12. Torben Hansen uploaded patch set 6 V +1 11/11/23, 13:00

  13. Torben Hansen commented on patch set 6 11/11/23, 13:02

    Reply

    Thanks for your feedback. I uploaded an improved patch which avoids code duplication in the `setOptions()` function so validators look less "bloated".
  14. core-ci voted on patch set 6 V -1 11/11/23, 13:10 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/48811

  15. Torben Hansen uploaded patch set 7 V -1 11/11/23, 13:20

  16. core-ci voted on patch set 7 V +1 11/11/23, 13:27 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/48812

  17. Torben Hansen wrote 11/11/23, 13:44

    Topic set to extbase
  18. Torben Hansen rebased onto patch set 8 V +1 11/22/23, 12:45

  19. core-ci voted on patch set 8 V +1 11/22/23, 12:51 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/49347

  20. Torben Hansen rebased onto patch set 9 V +1 12/02/23, 14:08

  21. core-ci voted on patch set 9 V +1 12/02/23, 14:16 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/50249

  22. Simon Schaufelberger voted on patch set 9 CR +1 12/22/23, 01:11

  23. Oliver Klee voted on patch set 9 V +1 12/22/23, 09:44

  24. Guido Schmechel commented on patch set 9 01/19/24, 20:45

    Feature-102326-AllowCustomTranslationsForExtbaseValidators.rst:66

    locallang_custom.xlf

    NumberRangeValidator.php:37

    Maybe "notInRangeTranslateKey"?
  25. Torben Hansen uploaded patch set 10 CR +1 V +1 01/20/24, 06:22

  26. Torben Hansen commented on patch set 9 01/20/24, 06:22

    Feature-102326-AllowCustomTranslationsForExtbaseValidators.rst:66 Reply

    Done

    NumberRangeValidator.php:37 Reply

    Agree, the `notInRangeTranslateKey` fits better here.
  27. core-ci voted on patch set 10 V +1 01/20/24, 06:27 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/51838

  28. Guido Schmechel voted on patch set 10 CR +1 01/20/24, 08:56

  29. Garvin Hicking voted on patch set 10 CR +1 01/20/24, 10:06

  30. Benni Mack commented on patch set 10 01/23/24, 22:16

    How about we change this to become more flexible. Instead of the $key and $extensionName, we could use a id such as "LLL:my_ext..." - we would need a new method next to translateErrorMessage(), or maybe just use LocalizationUtility directly? I think this might be beneficial to streamline the logic here and allow for overriding them via one line
  31. Torben Hansen commented on patch set 10 01/24/24, 07:28

    Reply

    The `LLL:EXT:` syntax is already supported here, so it is possible to override the translations with one line (also see examples in Feature RST). I however implemented both the `LLL:EXT:` syntax as well as the id + extensionName option here, since the latter would result in shorter line length for validator configuration and maybe also better readability. So basically developers have a choice to use one of both options. I'm also fine to change this patch as you suggested and remove the `$extensionName` option again. What do you think?
  32. Benni Mack commented on patch set 10 01/24/24, 09:07

    Reply

    yes. let's remove $extensionName again. The reason why is the following. Working with $extensionName and a simple labelKey is a problem because then the file would have to be named "locallang.xlf" and I would like to reduce this logic (and rather implement a new one based on Symfony/Translator, but that's another story, I have a ticket for this)
  33. Chris Müller uploaded patch set 11 CR +1 V +1 01/30/24, 18:59

  34. core-ci voted on patch set 11 V +1 01/30/24, 19:05 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/52509

  35. Torben Hansen uploaded patch set 12 V +1 02/01/24, 08:53

  36. Torben Hansen commented on patch set 12 02/01/24, 08:56

    Reply

    Done. Now we always use `LLL:EXT:` schema
  37. core-ci voted on patch set 12 V +1 02/01/24, 08:59 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/52572

  38. Guido Schmechel voted on patch set 12 CR +1 V +1 02/01/24, 16:22

  39. Simon Schaufelberger commented on patch set 12 02/02/24, 12:14

    Deprecation-102326-RegularExpressionValidatorValidatorOptionErrorMessage.rst:19

    and will be

    Deprecation-102326-RegularExpressionValidatorValidatorOptionErrorMessage.rst:50

    one space at the end can be removed
  40. Torben Hansen uploaded patch set 13 CR +1 V +1 02/02/24, 12:49

  41. Torben Hansen commented on patch set 12 02/02/24, 12:49

    Deprecation-102326-RegularExpressionValidatorValidatorOptionErrorMessage.rst:19 Reply

    Done

    Deprecation-102326-RegularExpressionValidatorValidatorOptionErrorMessage.rst:50 Reply

    Done
  42. core-ci voted on patch set 13 V +1 02/02/24, 12:54 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/52632

  43. Oliver Klee voted on patch set 13 V +1 02/02/24, 13:12

  44. Simon Schaufelberger voted on patch set 13 CR +1 V +1 02/02/24, 13:53

  45. Guido Schmechel voted on patch set 13 CR +1 V +1 02/02/24, 22:18

  46. Benni Mack voted on patch set 13 CR +1 02/05/24, 12:08

  47. Torben Hansen uploaded patch set 14 CR +1 V +1 02/27/24, 17:42

  48. core-ci voted on patch set 14 V -1 02/27/24, 17:48 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/53652

  49. Torben Hansen uploaded patch set 15 V -1 02/27/24, 17:52

  50. core-ci voted on patch set 15 V -1 02/27/24, 17:59 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/53655

  51. Torben Hansen uploaded patch set 16 V -1 02/27/24, 18:04

  52. core-ci voted on patch set 16 V +1 02/27/24, 18:11 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/53659

  53. Simon Schaufelberger voted on patch set 16 CR +1 V +1 03/20/24, 23:01

  54. Jörg Bösche voted on patch set 16 CR +1 03/25/24, 11:52

  1. Mathias Brodala commented on patch set 16 03/27/24, 09:15

    How about using "*Message" instead, similar to Symfony? See https://symfony.com/doc/current/validation/translations.html This would make it clear that using static strings instead of l10n references is also fine.
  2. Torben Hansen commented on patch set 16 03/27/24, 09:20

    Reply

    You mean `message` instead of `translateKey`? If yes, I`m fine with that. For validators with multiple messages (e.g. NumberRangeValidator), we can use `message` as postfix (e.g. `notValidMessage` and `notInRangeMessage`)
  3. Torben Hansen uploaded patch set 17 CR +1 V +1 03/28/24, 18:10

  4. core-ci voted on patch set 17 V +1 03/28/24, 18:16 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/55041

  5. Torben Hansen commented on patch set 17 03/28/24, 18:19

    Reply

    Renamed the option and updated RST files.
  6. Mathias Brodala commented on patch set 17 04/02/24, 12:03

    AbstractValidator.php:128

    This should be updated or removed, the method does more than this by now.
  7. Torben Hansen uploaded patch set 18 V +1 04/02/24, 17:51

  8. Torben Hansen commented on patch set 18 04/02/24, 17:52

    AbstractValidator.php:128 Reply

    Done
  9. Simon Schaufelberger wrote 04/02/24, 17:52

    Removed reviewer Simon Schaufelberger.
  10. core-ci voted on patch set 18 V +1 04/02/24, 17:57 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/55187

  11. Guido Schmechel voted on patch set 18 CR +1 V +1 04/03/24, 10:02

  12. Jörg Bösche voted on patch set 18 CR +1 04/04/24, 06:31

  13. Garvin Hicking commented on patch set 18 CR +1 V +1 04/05/24, 10:05

    FINALLY had the time to actually test this, works, and looks like a very nice convenience. Thanks for staying on it and iterating to the current solution! In the commit message there's a reference left to the initial variant `translateKey` that needs to be addapted to `messages`. Other than that, a few minor RST-comments.

    Deprecation-102326-RegularExpressionValidatorValidatorOptionErrorMessage.rst:45

    The old changelogs and most of the documentation often uses: `use TYPO3\CMS\Extbase\Annotation as Extbase;` and then `#[Extbase\Validate([` instead. Maybe it would be good to follow that (see i.e. `typo3/sysext/extbase/Tests/Unit/Reflection/Fixture/DummyController.php` or `typo3/sysext/core/Documentation/Changelog/12.0/Feature-96688-AttributesForExtbaseAnnotations.rst`. Using the probably more common syntax (albeit longer) might people better help to grep their sources for a needed change. Another thing: You only describe this using PHP attributes. Would mentioning the alternative of phpDoc comments be worthwile here, also to allow people easier spotting @Validate(...) / @Extbase\Validate(...) notations they would need to adapt?

    Feature-102326-AllowCustomTranslationsForExtbaseValidators.rst:15

    Maybe strike this one "by validators" to have the sentence with only 2x "validator" words. I appreciate the specificity of this though!

    Feature-102326-AllowCustomTranslationsForExtbaseValidators.rst:20

    Maybe add an example of those instances: "(like `Boolean`, `NotEmpty`, `NumberRange`)".

    Feature-102326-AllowCustomTranslationsForExtbaseValidators.rst:29

    (see other comment about the `Extbase\Validate` usage instead)
  14. Torben Hansen uploaded patch set 19 CR +1 V +1 04/12/24, 09:11

  15. Torben Hansen commented on patch set 18 04/12/24, 09:11

    Deprecation-102326-RegularExpressionValidatorValidatorOptionErrorMessage.rst:45 Reply

    Changed. I would hovever not mention `@Extbase\Validate` here any more, since IMO php attributes should be the way to go.

    Feature-102326-AllowCustomTranslationsForExtbaseValidators.rst:15 Reply

    Done

    Feature-102326-AllowCustomTranslationsForExtbaseValidators.rst:20 Reply

    Done

    Feature-102326-AllowCustomTranslationsForExtbaseValidators.rst:29 Reply

    Done
  16. core-ci voted on patch set 19 V +1 04/12/24, 09:38 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/55844

  17. Garvin Hicking voted on patch set 19 CR +1 V +1 04/12/24, 09:40

  18. Guido Schmechel voted on patch set 19 CR +1 V +1 04/12/24, 12:12

  19. Torben Hansen uploaded patch set 20 CR +1 V +1 04/27/24, 13:00

  20. core-ci voted on patch set 20 V +1 04/27/24, 13:05 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/56854

  21. Garvin Hicking voted on patch set 20 CR +1 V +1 04/27/24, 19:47

  22. Oliver Klee voted on patch set 20 V +1 04/28/24, 07:53

  23. Simon Schaufelberger voted on patch set 20 CR +1 V +1 04/29/24, 09:28

  24. Benni Mack voted on patch set 20 CR +2 V +2 06/07/24, 10:34

  25. Benni Mack merged patch set 21 06/07/24, 10:34