-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Support "{{ label }}" placeholder in error messages #12238
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
Comments
👍 If it also goes through |
+1 |
2 similar comments
👍 |
👍 |
Where exactly in the process should this be converted do you think? The ViolationMapper adds the FormError with the $message/$messageTemplate. I suppose the message should be transformed/translated at that point or earlier? |
Sad to see this issue has become a bit outdated, we could really use this functionality in our projects! With a few pointers on the approach I'd be happy to submit a PR for this so this can be fixed. @webmozart would you know if @ErikTrapman suggestion is on the right track? I can have a go with that to start with. |
so there is still no pull request? I would work on this issue if no one objects |
Please do and do not hesitate to ask if you need any help. :) |
Wow.. I just discovered this issue.. I thought that not supporting Let me know also.. otherwise I would also be willing to work on this... |
working on this already, I'll give a feedback in case there will be any problems |
If you look the Symfony\Component\Validator\Constraints\Notblank class is empty, but you can add a custom placeholder.... by extends it and create yout custom validator like this. According to the documentation (https://symfony.com/doc/current/validation/custom_constraint.html), first we create a Constraint class by extends NotBlank. // src/Validator/Constraints/NotBlankWithPlaceholder.php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* @Annotation
*/
class NotBlankWithPlaceholder extends NotBlank
{
/**
* @var string
*/
public $placeholder;
} After we create (into the same folder of custom constraint class, the validator like this // src/Validator/Constraints/NotBlankWithPlaceholderValidator.php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class NotBlankWithPlaceholderValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof NotBlankWithPlaceholder) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NotBlankWithPlaceholder');
}
if (false === $value || (empty($value) && '0' != $value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ placeholder }}', $constraint->placeholder)
->setCode(NotBlankWithPlaceholder::IS_BLANK_ERROR)
->addViolation();
}
}
} After we use translate validation constraint messages by creating a validators catalog into the translations directory (see https://symfony.com/doc/current/validation/translations.html) # translations/validators.fr.yaml
code:
not_blank:
male: Le code d'un {{ placeholder }} ne peut être vide !
female: Le code d'une {{ placeholder }} ne peut être vide ! And finaly we can use the custom validator into your entity like this. Don't forget to add namespace of your custom contraint with // src/Entity/Domaine.php
/**
* @ORM\Table()
* @ORM\Entity(repositoryClass="App\Repository\DomaineRepository")
*/
class Domaine
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=10)
* @AppAssert\NotBlankWithPlaceholder(
* message = "code.not_blank.male",
* placeholder = "domaine"
* )
*/
private $code; You can change placeholder with label, and you can imagine with an array collection placeholders in order to pass multiple variables, as you can do with sprintf. ^^. |
Yes we did the same thing... @v20100v if I am not mistaken one more problem with the above approach is that the This enforced us to inject in each validator the translator which made the code ugly. |
What behavior is implied if the validation runs manually, not through form component? Maybe we should add new constraint option, such as |
After some research I can offer such a solution. @xabbuh what do you think about this? |
It Is possible ti work on this ? |
…in constraint messages (a-menshchikov) This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- Added support for using the "{{ label }}" placeholder in constraint messages | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #12238 | License | MIT | Doc PR | - [ ] Add docs PR Commits ------- 0d9f442 Added support for using the "{{ label }}" placeholder in constraint messages
Origin of this ticket: #5714
Currently, it is not possible to write error messages that contain the name of the field that they concern. Technically, that's logical, since the Validator component is not aware of the Form component.
However, it would be quite simple to support "{{ label }}" placeholders (or similar). These could be set by the validator to the field's name by default ("title" in the below example), and overwritten in the Form context by the corresponding field's label:
The text was updated successfully, but these errors were encountered: