Easiest Way to Customize a Symfony Form

Before I get started, I realize that Symfony has this entire form theming apparatus and it’s great. But this is a simple technique that puts the control of the form in the hands of the person laying out the HTML rather than the developer.

All I do is when I’m rendering my form, I wrap the widget and label and errors, if applicable, in a fieldset element. I then add a class to that fieldset element.

<fieldset class="input-url">
  {{ form_label(form.myurl) }}
  {{ form_widget(form.myurl) }}

This renders this HTML.

<fieldset class="input-url">
<label>Billing WSDL</label>
<input type="text" name="myurlname" required="required" value="" />

That looks good to me.

Now this input element can be styled accordingly. In this instance, I just want the input text box to be longer to hold a website address. I just set the width in CSS, it’s actually SASS, but the CSS end product looks like this.

.input-url input {
Share Button