Reputation: 2962
This is mostly for curiosity, but I would love to be able to do this.
Basically I have a situation where I would like to enter an autofocus
into a form using Symfony's FormBuilder. It is possible to enter this by using something like this
->add('textField', null,
array('attr' => array('class' => 'text-field-ex', 'autofocus' => true)))
However, as a result, this ends up producing autofocus="1"
in the resulting form. This works fine for Chrome, but Firefox does not like this format; it prefers autofocus
to live alone, and not as an attribute; hence it does not work. This is easy to do in raw php code, but for some reason Symfony does not want to do this.
I know this has been (sort of) addressed here, and as is suggested in the comments, this can be handled relatively easily with Javascript, this to me seems like an imperfect solution, due to inherent instabiity. I have it working this way, but I'd rather have the code itself be rendered properly the first time.
I have tried something like this:
$autofocus = 'autofocus';
->add('textField', null,
array('attr' => array('class' => 'text-field-ex', $autofocus)))
But this simply turns the resulting form field to be "0"=autofocus
and still does not work.
Does anyone have any ideas on how to handle this directly in Symfony?
Upvotes: 2
Views: 89
Reputation: 3432
You could just override the block_widget
in your templates (see http://symfony.com/doc/current/form/form_customization.html) and replace the way it outputs attributes with something along those lines :
{% for attrname, attrvalue in attr %}
{% if attrname == 'autofocus' %}
autofocus
{% else %}
{{ attrname }}="{{ attrvalue }}"
{% endif %}
{% endfor %}
It's cleaner than a javascript solution I guess.
Upvotes: 1