Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Symfony\Form) prevent form fields from being generated automaticly

Tags:

twig

symfony

I've got a twig-template. It iterates through the collection of forms (form.MeteringCodes). The problem is, i need to display only the value of one field and when i dont use "form_row(...)" explicitly, symfony generates the field automaticly at the end of the form.

    {{ form_start(form) }}
...
            <tbody>
                {% for metCode in form.MeteringCodes %}
                    {{ form_row(metCode.id) }}
                <tr>
                    <td>{{ metCode.desc.vars.value }} {# <-- Here is the pure value #}</td>
                    <td>{{ form_row(metCode.sdesc) }}</td>
                    <td style="width:20%;">{{ form_row(metCode.type) }}</td>
                    <td style="width:15%;">{{ form_row(metCode.state) }}</td>
                </tr>
                {% endfor %}
             </tbody>
...
        {{ form_end(form) }}

I see two ways. One is to disable this "auto generating" behaviour. The second might be if i just get the value from the original object itself, without defining it as a field (if its possible).

Collection type itself:

class MeteringCodeType extends \Symfony\Component\Form\AbstractType
{
    public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
    {
        $builder->add('id', 'hidden');
        $builder->add('desc', 'text', ['label' => false, 'disabled' => true]);
        $builder->add('sdesc', 'text', ['label' => false]);
        $builder->add('type', 'choice', ['choices'  => array('L' => '...', 'D' => '...', 'N' => '...'), 'label' => false]);
        $builder->add('state', 'text', ['label' => false]);
    }

    public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'app\models\MeteringCode',
        ));
    }

    public function getName()
    {
        return 'MeteringCode';
    }
}

I dont want to use some workarounds like a hidden field.

like image 863
Sergey Bratenkov Avatar asked Sep 15 '15 11:09

Sergey Bratenkov


3 Answers

You can pass additional argument to form_end() call:

{{ form_end(form, {'render_rest': false}) }}

Hope this helps...

like image 70
Jovan Perovic Avatar answered Nov 10 '22 12:11

Jovan Perovic


Nice approach @Sergey Bratenkov I wanted it to show it with a text field in read only mode because my field was an entity field. This is what I used

<input type="text" value="{{ formulario.vars.value.getJuego() }}" readonly="readonly"/>

{% do formulario.juego.setRendered %}

with setRendered functionality you tell twig to stop rendering the field automatically after using form_start.

like image 31
Rodolfo Velasco Avatar answered Nov 10 '22 12:11

Rodolfo Velasco


Thx everybody. I found the solution. I removed the field from the form itself, and accessing the value through the getter of an original object. Another benefit is that the field does not need to be rendered at all.

{{ metCode.vars.value.getDesc() }}

like image 1
Sergey Bratenkov Avatar answered Nov 10 '22 13:11

Sergey Bratenkov