简体   繁体   English

Symfony Validator Standalone-HTML属性

[英]Symfony Validator Standalone - HTML Attributes

I am using the SF2 forms and validator bundle by themselves. 我自己使用SF2表单和验证程序包。 Is there anyway to generate HTML attributes off annotations that are created with the symfony validator? 无论如何,是否可以使用symfony验证程序创建的注释生成HTML属性? For example: 例如:

Convert this - 转换-

/**
 * @var
 * @Assert\Length(min="2", max="2")
 */
protected $state;

into the HTML code 进入HTML代码

<input type="text" maxlength="2" ... >

Typically, I know that is set in SF2 by setting a size for the string in a Doctrine annotation, but I am using Aura.SQL to handle interaction with the database. 通常,我知道这是通过在Doctrine注释中设置字符串的大小来在SF2中设置的,但是我正在使用Aura.SQL处理与数据库的交互。

Thanks for any information! 感谢您提供任何信息!

I think that whate you are looking for does not exists. 我认为您正在寻找的东西不存在。 By the way, you can implement it by creating your own form type guesser 顺便说一句,您可以通过创建自己的表单类型猜测器来实现它

Well, according to the documentation , the "guessing" is activated when you omit the second argument to the add() method (or if you pass null to it). 好吧,根据文档 ,当您将第二个参数省略给add()方法(或者如果将null传递给它)时,“猜测”被激活。

If you use this feature, Symfony will try to guess the "type" for a field and the correct values of a number of field options based on validation metadata (and doctrine info). 如果使用此功能,Symfony会根据验证元数据(和学说信息)尝试猜测字段的“类型”以及许多字段选项的正确值。

So, in theory, it should give you the expected output if you omit the second argument or pass null to it (it would render the html5 attributes in the outputted input ). 因此,从理论上讲,如果省略第二个参数或将null传递给它,它将为您提供预期的输出(它将在输出的input呈现html5属性)。

If for any reason it doesn't work, you can always force its rendering in the add method (it also overrides any guessing if it existed): 如果由于某种原因它不起作用,您始终可以在add方法中强制其呈现(它还会覆盖所有猜测,如果存在的话):

->add('state', null, array('attr' => array('minlength' => 2,'maxlength' => 2)))

or even in the twig template: 甚至在树枝模板中:

{{ form_widget(form.state, {'attr': {'maxlength' : 2, 'minlength' : 2 } }) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM