symfony - Symfony2 Change class name in twig form theme -
i want override form_widget_simple
function
{% block form_widget_simple %} {% spaceless %} {% set type = type|default('text') %} {% if errors|length > 0 %} {{dump(form.vars.attr)}} {% endif %} <input type="{{ type }}" {{ block('widget_attributes') }} {% if value not empty %}value="{{ value }}" {% endif %}/> {% endspaceless %} {% endblock form_widget_simple %}
but dont know how set form.vars.attr['class']
inside if
statement whent set form.vars.attr['class'] = 'error';
error unexpected token "punctuation" of value "." ("end of statement block" expected)
as see, adding additional attributes handled in widget_attributes
block. if take in there, see simple foreach on attr
array, attributes. think simple set merging existing one, done. form_widget_simple
block like
{% block form_widget_simple %} {% spaceless %} {% set type = type|default('text') %} {% if errors|length > 0 %} {{dump(form.vars.attr)}} {% endif %} {% set attr = attr|merge({'class': (attr.class|default('') ~ ' your-css-class')|trim}) %} <input type="{{ type }}" {{ block('widget_attributes') }} {% if value not empty %}value="{{ value }}" {% endif %}/> {% endspaceless %} {% endblock form_widget_simple %}
this preserve every class attribute set in form builder , add your-css-class
additional class. if no class attribute defined, your-css-class
set.
Comments
Post a Comment