I would like to only show some parts of a form, but the form is always fully rendered, with all the fields, instead of just showing the fields I want to display. Would you know why?
{{ form_start(form) }}
{{ form_label(form.title) }}
{{ form_end(form) }}
$form = $this->createFormBuilder($advert)
->add('date', DateType::class, array(
'widget' => 'text',
'label' => 'custom Date',
))
->add('title', TextType::class)
->add('save', SubmitType::class)
->getForm();
return $this->render('OCPlatformBundle:Advert:edit.html.twig', array(
'form' => $form->createView(),
'advert' => $advert
));
The result is : the date and the title are displayed, both with the label and the html value.
Thanks
form_end()
also outputs form_rest()
, which renders all fields that have not yet been rendered for the given form: http://symfony.com/doc/current/reference/forms/twig_reference.html#form-rest-view-variables
If you don't want to render the unrendered fields, add {'render_rest': false}
to form_end
:
{{ form_end(form, {'render_rest': false}) }}
You can display only some part of form
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.first) }}
{{ form_row(form.second) }}
...
{{ form_row(form.submit, { 'label': 'Submit me' }) }}
{{ form_end(form, {'render_rest': false}) }}
If you wanna remove fields, you can remove fields in Controller example:
$form->remove("your fields");