简体   繁体   English

symfony2表单禁用

[英]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: 在我的控制器的showAction()中,我有以下代码:

$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: show.html.twig中的一个例子是:

{% 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. 不幸的是,父表单上的read_only设置似乎没有级联到包含的子表单。 Is there a way to handle this? 有办法处理这个吗?

Try: 尝试:

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

See: vendor/symfony/symfony/src/Symfony/Component/Form/CHANGELOG.md, first line 请参阅:vendor / symfony / symfony / src / Symfony / Component / Form / CHANGELOG.md,第一行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM