1
votes

I want to apply a css class 'pure-control-group' to all div of a generated form.

<form name="form" method="post" class="pure-form pure-form-aligned">
    <div>
        <label for="form_Title" class="required">Titre</label>
        <select id="form_Title" name="form[Title]" class="pure-control-group">            
            <option value="Modification" >Modification</option>
            <option value="Construction" >Construction</option>
            <option value="Autre" >Autre</option>
        </select>
    </div>
    <div>
        <label for="form_ContactWay" class="required">Moyen de Contact</label>
        <select id="form_ContactWay" name="form[ContactWay]" class="pure-control-group">            
            <option value="Telephone" >Téléphone</option>            
            <option value="Email" >Email</option>            
            <option value="Direct" >Direct</option>            
            <option value="Autre" >Autre</option>
        </select>
    </div>
    <div>
        <label for="form_Log" class="required">Journal</label>
        <textarea id="form_Log" name="form[Log]" required="required" class="pure-control-group"></textarea>
    </div>
    <div>
        <button type="submit" id="form_Enregistrer" name="form[Enregistrer]">Enregistrer</button>
    </div>
    <input type="hidden" id="form__token" name="form[_token]" value="c19WunU5AgDgc954I3DRJXLqEhQwpOyDCBZEpF7akJs" />
</form>

I tried :

$this->logForm = $this->createFormBuilder($log, array('allow_extra_fields' => true))
->add('Title',          ChoiceType::class, array(
    'label' => 'Titre',
    'choices' => array(
        'Modification' => 'Modification',
        'Construction' => 'Construction',
        'Autre' => 'Autre'),
    'attr'=> array('class'=>'pure-control-group')))
->add('ContactWay',     ChoiceType::class, array(
    'label' => 'Moyen de Contact',
    'choices' => array(
        'Téléphone' => 'Telephone',
        'Email' => 'Email',
        'Direct' => 'Direct',
        'Autre' => 'Autre'),
    'attr'=> array('class'=>'pure-control-group')))
->add('Log',        TextareaType::class, array(
    'label'=> 'Journal',
    'attr'=> array('class'=>'pure-control-group')))
->add('Enregistrer',    SubmitType::class)
->getForm();

The problem is that the class is add to the input. label_attr do the job but for the label.

How can I do for the div?

Notice that I would appreciate not to render each field by hand.

1
What div are you talking about? - D4V1D
I've edited with the automated generated form. - Valentin BEAULE

1 Answers

2
votes

You can modify the template for the form row. Do this in the template that the form is rendered in:

{% form_theme form _self %}

{%- block form_row -%}
    <div class="pure-control-group">
        {{- form_label(form) -}}
        {{- form_errors(form) -}}
        {{- form_widget(form) -}}
    </div>
{%- endblock form_row -%}