How to disable a field in edit view using Symfony 2 FormBuilder
I created a form with Symfony2 FormBuilder and I want to disabled one of
the fields in the edit view. I'm actually hiding it with a wrapper
(display:none) but I was wondering if there's a better way to do this. My
code looks like the following:
EntityType
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('fieldToDisabledInEditView');
// ...
EntityController
public function newAction() {
$entity = new Entity;
$form = $this->createForm(new EntityType, $entity);
// ...
}
public function editAction() {
$entity = new Entity;
$form = $this->createForm(new EntityType, $entity);
// ...
}
New (twig) Template
<form>
{{ form_row(form.fieldToDisabledInEditView) }}
{# ... #}
Edit (twig) Template
<form>
<span class="theValueOfTheHiddenField">{{
entity.fieldToDisabledInEditView }}</span>
<div style="display:none">
{{ form_row(form.fieldToDisabledInEditView) }}
</div>
{# ... #}
No comments:
Post a Comment