how to add a css class type to a filed,

recording to the post of stack overflow:

Here is the way you can do it when building your form,
 $builder->add('field_name', 'text', array('label' => 'Field Label', 'attr' => array('class' => 'fieldClass')));
You can also do it when rendering your form fields, take a look at Twig template form function reference
{{ form_label(form.field, 'label', { 'attr': {'class': 'foo'} }) }}
{{ form_widget(form.field, { 'attr': {'class': 'bar'} }) }}
You can then add a css file in your bundle public assets and call it in your template using,
{% block stylesheets %}
    {% parent() %} {# if you want to keep base template's stylesheets #}
    bundle/myBundle/css/stylesheet.css') }}" type="text/css" rel="stylesheet" />
{% endblock %}
You've then to make your public assets be accessible through your web/ directory. The best way to do it is to create symbolic links targeting your bundle public assets, you've then to execute
assets:install web/ --symlink
Another useful approach when you want to thoroughly customize specific form field rendering block (Twig) is to define a new form theme, here's the documentation > Form theming in Twig.

评论

热门帖子