I need to add an attribute to a form field in symfony.
I do it like this in my form type:
->add('myfield','text',array('attr'=>array('myattrib'=>"test")))
But this overwrites defult attributes
of that field (e.g. classes)
How can I add an attribute to a field without overwriting other attributes?
Thanks
you can add it using twig if you wanna just to keep your php code clear, here is an exemple :
{{ form_row(form.Address,{
'attr':{
'class':'form-control',
'min-length':'4',
'required':'true',
}
}) }}
In your form add the attribute like this :
->add('myfield','text',array('attr'=>array('myattrib'=>"test"),'mapped'=>false))
Otherwise you can add it in your twig view when rendering you form with form_row() just add the input like normale html and get int in your action after posting like this :
$posted_value = $this->get('request')->request->get('Name attribute of your input')