目前,我正在使用实体类中的注释使用 Symfony Validator 组件来实现验证。
Symfony 文档显示您可以使用某些占位符通过消息传递变量,并且这些消息可以由 Translator 组件进行翻译。
例如,可以编写以下注释:
/**
* Assert\Length(
* min = 5,
* max = 10,
* minMessage = "Title too short: {{ limit }}",
* maxMessage = "Title too long: {{ limit }}"
*/
protected $title;
这很好用,但我想知道您可以使用哪些类型的占位符?
是否可以创建自定义占位符?
我知道{{ value }}
占位符存在(并且有效),但它不在 Symfony 的有关如何使用长度验证的文档页面上。
我想使用占位符标签,例如{{ key }}
or {{ name }}
传递字段的技术名称(在本例中为“标题”),这样我就可以写我的minMessage
as minMessage = "{{ field }} too short: {{ limit }}"
我尝试检查 Symfony 的标准组件以了解如何处理这些占位符,但我找不到可用的变量的正确列表。
请帮我! T_T
如果您查看作为示例发布的 LengthValidator 的代码,您可以看到这些“变量”只是在其自己的 Validator 类中替换的静态字符串。
像这样,all其中一些是自定义的,这可能也是没有可用列表的原因。
班上:
https://github.com/symfony/Validator/blob/master/Constraints/LengthValidator.php 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)