Symfony2 Validator Post: Which variables are available?

I am currently performing validation using the Symfony Validator component using annotations in the Entity class.

In the Symfony documentation, you can use certain placeholders to pass variables through a message, and these messages can be translated by the Translator component.

So, for example, you can write the following annotation:

/** * Assert\Length( * min = 5, * max = 10, * minMessage = "Title too short: {{ limit }}", * maxMessage = "Title too long: {{ limit }}" */ protected $title; 

This works great, but I was wondering what types of placeholders are available to you? Is it possible to create custom placeholders?

I know that the {{ value }} placeholder exists (and works), but it is not on the Symfony documentation page on how to use length checking.

I would like to use a placeholder such as {{ key }} or {{ name }} to pass in the technical name of the field (in this case "title"), so I can write minMessage as minMessage = "{{ field }} too short: {{ limit }}"

I tried to check the standard components for Symfony to see how these placeholders are handled, but I cannot find the correct list of variables available to me.

Please help me! T_T

+7
variables placeholder validation symfony
source share
1 answer

If you check the code for the LengthValidator that you sent as an example, you can see that these “variables” are just static strings that are replaced inside the Validator's own class.

Thus, they are all ordinary, which is also possible because the list is missing.

Grade:

https://github.com/symfony/Validator/blob/master/Constraints/LengthValidator.php

Corresponding fragment:

 if (null !== $constraint->max && $length > $constraint->max) { $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage) ->setParameter('{{ value }}', $this->formatValue($stringValue)) ->setParameter('{{ limit }}', $constraint->max) ->setInvalidValue($value) ->setPlural((int) $constraint->max) ->setCode(Length::TOO_LONG_ERROR) ->addViolation(); return; } 
+7
source share

All Articles