validator icon indicating copy to clipboard operation
validator copied to clipboard

Add to each custom message in rules parameter for set processing type

Open vjik opened this issue 1 year ago • 11 comments

Default is Error::MESSAGE_TRANSLATE.

For example, Count rule:

public function __construct(
  // ...
  
  string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{item} other{items}}.',
  int $greaterThanMaxMessageProcessing = Error::MESSAGE_TRANSLATE,
  
  // ...

vjik avatar Aug 28 '24 08:08 vjik

Isn't that too much granularity to set that per each message? I think, usually it is turned on/off on whole validator level, not per rule.

samdark avatar Aug 28 '24 11:08 samdark

By default, built-in messages that require translation are used. If my app is single language, then I can set one concrete message on my language and set proccessing for this message only. Other message should continue using translation.

vjik avatar Aug 29 '24 08:08 vjik

If you set message in your language but not change message processing nothing changes, isn't it? Translator can't handle it and leaves it as is

BoShurik avatar Aug 29 '24 09:08 BoShurik

If you set message in your language but not change message processing nothing changes, isn't it? Translator can't handle it and leaves it as is

No. Translator also uses formatter and format message in original language when not found translation.

Real example.

Russian is application language.

Translator configuration is default. Validator configuration:

ValidatorInterface::class => [
    'class' => Validator::class,
    '__construct()' => [
        'ruleHandlerResolver' => Reference::to(RuleHandlerContainer::class),
        'messageFormatter' => Reference::to(IntlMessageFormatter::class),
        'messageFormatterLocale' => 'ru-RU',
    ],
],

Rule:

 #[Count(
	max: self::LIMIT,
	greaterThanMaxMessage: 'Превышен лимит в {max, number} {max, plural, one{элемент} few{элемента} other{элементов}}.'
)]

When limit is "21", message will be "Превышен лимит в 21 элементов." (Original language for translator is english). Expected message "Превышен лимит в 21 элемент." (Original language for app is russian). I need to set Error::MESSAGE_FORMAT processing to this message only.

vjik avatar Aug 29 '24 11:08 vjik

If it's a single language app then you must set this language for translator in the configuration so you get expected message

BoShurik avatar Aug 29 '24 12:08 BoShurik

If it's a single language app then you must set this language for translator in the configuration so you get expected message

Yes. But for translator language of original messages is English, but custom original messages in app on Russian.

vjik avatar Aug 29 '24 13:08 vjik

Yes. But for translator language of original messages is English, but custom original messages in app on Russian.

Custom messages you write in Russian, validator build-in messages are translated to Russian from English because you set Russian locale

BoShurik avatar Aug 29 '24 14:08 BoShurik

Custom messages you write in Russian, validator build-in messages are translated to Russian from English because you set Russian locale

No. Translator tried translate to russian, but it not found translation and format original message (on russian) with translator original language (english).

vjik avatar Aug 29 '24 14:08 vjik

with translator original language (english)

Why it's english if app's locale is russian?

BoShurik avatar Aug 29 '24 14:08 BoShurik

Why it's english if app's locale is russian?

Default messages in English (example), so for translator original language is english.

vjik avatar Aug 29 '24 14:08 vjik

But there is translation for this message. It wouldn't be translated?

BoShurik avatar Aug 29 '24 14:08 BoShurik