I have a simple FormType
attached to an entity called media
wich 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?