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.
You can pass additional argument to form_end()
call:
{{ form_end(form, {'render_rest': false}) }}
Hope this helps...
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.
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() }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With