Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 form disable

I am using the same form to "preview" an object as I am to "edit/update" the same object. In my showAction() for the controller I have the following code:

$form = $this->createForm(new SalesEntityType($entity), $entity, array('read_only' => true) );

This code works great for the primary form but there are a number of subforms that are made part of this by inclusion. One example in the show.html.twig is:

{% include 'TargetCommonBundle:Hours:hoursForm.html.twig' with { form: hours } %}

Unfortunately, the read_only setting on the parent form does not seem to cascade to the included subforms. Is there a way to handle this?

like image 626
Cosmtar Avatar asked Dec 09 '12 16:12

Cosmtar


2 Answers

Try:

$form = $this->createForm(
    new SalesEntityType($entity),
    $entity,
    [ 'disabled' => true ]
);

See: vendor/symfony/symfony/src/Symfony/Component/Form/CHANGELOG.md, first line

like image 109
Lighthart Avatar answered Oct 16 '22 00:10

Lighthart


// It is the way more fast to disabled a form
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->setDisabled(true);
}    
like image 23
hvallenilla Avatar answered Oct 16 '22 00:10

hvallenilla