在不覆盖symfony2中默认值的情况下向表单字段添加属性


adding an attribute to form field without overwriting defaults in symfony2

我需要在symfony中的表单字段中添加一个属性
我在我的表单类型中是这样做的:

->add('myfield','text',array('attr'=>array('myattrib'=>"test")))

但这会覆盖该字段(例如类)的取消填充attributes

如何在不覆盖其他属性的情况下将属性添加到字段?

感谢

如果你只想保持php代码的清晰,你可以使用trick添加它,这里有一个例子:

{{ form_row(form.Address,{
                'attr':{
                    'class':'form-control',
                    'min-length':'4',
                    'required':'true',
                }
            }) }}

在表单中添加如下属性:

->add('myfield','text',array('attr'=>array('myattrib'=>"test"),'mapped'=>false))

否则,你可以在用form_row()呈现表单时将其添加到你的树枝视图中,只需添加像normale html这样的输入,并在发布后在你的操作中获取int:

$posted_value = $this->get('request')->request->get('Name attribute of your input')