13

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?

3
  • It seems that the read_only works fine in the subforms as it does in the main form. It is just that the none of the drop-down lists in the main or subforms are disabled from being changed. Any help appreciated.
    – Cosmtar
    Commented Dec 10, 2012 at 4:53
  • Can you add the read_only variable to the with statement? ie: with { form: hours, read_only: read_only }
    – lifo
    Commented Dec 10, 2012 at 12:28
  • Try: $form = $this->createForm(new SalesEntityType($entity), $entity, array('disabled' => true) );
    – Lighthart
    Commented Dec 10, 2012 at 17:27

2 Answers 2

18

Try:

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

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

0
3
// It is the way more fast to disabled a form
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->setDisabled(true);
}    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.