Reputation: 774
I have a simple FormType
attached to an entity called media
which I rendered in my view.
I have a newAction
that lets me create my object, and a editAction
that lets me edit it with my same form in my controller.
However I don't want some field appears in my edit
view` as I already entered them when I created it.
But even though I use form_row
to specifically render my form line by line, when I add the form_end
at the end, it renders all my fields, even the ones I didn't call.
My FormType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', FileType::class, array(
'data_class' => null,
'label' => "Add an image"
))
->add('context', ChoiceType::class, array(
'label' => 'image section',
'choices' => array(
'header' => 'header',
'middle' => 'middle',
)
))
->add('save', SubmitType::class, array(
'label' => "Add"
));
}
My view
{{ form_start(editForm) }}
{{ form_row(editForm.name) }}
{{ form_row(editForm.save) }}
{{ form_end(editForm) }}
But even if I use the form rows, it actually shows my context
field in the view, which I didn't call.
So I tried some hack to get around it.
this one worked but when I click on submit
form, it shows me an error that context
field cannot be null, so this doesn't do the trick
{% do editForm.context.setRendered %}
And I found a way to do it with jQuery to hide the form like this
<script>
$(document).ready(function () {
$("#media_context").parent().hide();
});
</script>
the jQuery works and hide my row in my form. But I was wondering if I could do it without using jQuery and be able to render only specific field of my form in my view?
Upvotes: 0
Views: 2792
Reputation: 799
From the symfony docs:
This helper (form_end) also outputs form_rest() unless you set render_rest to false.
form_rest(view, variables)
This renders all fields that have not yet been rendered for the given form.
{# don't render unrendered fields #}
{{ form_end(form, {'render_rest': false}) }}
Upvotes: 3
Reputation: 7055
Try this
{{ form_start(editForm) }}
{{ form_row(editForm.name) }}
{{ form_row(editForm.save) }}
{{ form_end(editForm, {'render_rest': false}) }}
Upvotes: 1
Reputation: 2885
In Symfony 2, you could remove some fields from the builder when editing your entity. Your edit form must extends
your create form in Symfony 2.
I think you can do the same in Symfony 3, try something like :
class EditType extends CreateType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->remove('context') //remove the fields that no longer needed
}
public function configureOptions(OptionsResolver $resolver)
{
/...
}
}
You don't need to change CreateType
class Createtype extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', FileType::class, array(
'data_class' => null,
'label' => "Add an image"
))
->add('context', ChoiceType::class, array(
'label' => 'image section',
'choices' => array(
'header' => 'header',
'middle' => 'middle',
)
))
->add('save', SubmitType::class, array(
'label' => "Add"
));
}
}
Upvotes: 5