Symfony2验证器消息:哪些变量可用? [英] Symfony2 Validator message: Which variables are available?

查看:125
本文介绍了Symfony2验证器消息:哪些变量可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在使用Symfony Validator组件通过Entity类中的Annotations实现验证.

At this moment I am implementing validation using the Symfony Validator component using Annotations in my Entity class.

Symfony文档显示,您可以使用某些占位符通过消息传递变量,并且这些消息可以由翻译器组件进行翻译.

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

例如,可以编写以下注释:

So for example, it's possible to write the following annotation:

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

这很好,但是我想知道您可以使用哪种占位符? 是否可以创建自定义占位符?

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

我知道{{ value }}占位符存在(并且可以工作),但是关于如何使用Length验证,它不在Symfony的文档页面上.

I know that the {{ value }} placeholder exists (and works), but it's not on the documentation page of Symfony on how to use the Length validation.

我想使用诸如{{ key }}{{ name }}的占位符标记来传递字段的技术名称(在本例中为"title"),因此我可以将minMessage编写为minMessage = "{{ field }} too short: {{ limit }}"

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

我试图检查Symfony的标准组件,以了解如何处理这些占位符,但是我找不到适合我的变量的正确列表.

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

请帮助我! T_T

推荐答案

如果您查看作为示例发布的LengthValidator的代码,则可以看到这些变量"只是静态字符串,并在其自己的Validator中替换了课.

If you check out the code for the LengthValidator you posted as an example you can see that these "variables" are just static strings that are replaced inside their own Validator class.

因此,它们中的全部是自定义的,这也可能就是为什么没有可用列表的原因.

As such, all of them are custom, which is possibly also why there isn't a list available.

课程:

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

相关代码段:

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;
        }

这篇关于Symfony2验证器消息:哪些变量可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆