[英]Disabling a text box depending of a condition in symfony formType
I'm pretty new in Symfony2 and I have a problem with a form type. 我在Symfony2中很陌生,并且表单类型有问题。
I have the table 'messages' in the data base and every message have an id, message and owner. 我在数据库中有表“ messages”,每个消息都有一个id,消息和所有者。
Then, I have to show these messages inside a forms. 然后,我必须在表单中显示这些消息。 So I do this: 所以我这样做:
->add('shortComment', 'text', array(
'label' => 'Short Message',
'data' => $this->_predefinedMessage->getShortComment()))
So, the problem for me is when I want to disable this text box when: 所以,对我来说,问题是当我想在以下情况下禁用此文本框时:
owner != currentUser
I don't know how to preceed. 我不知道该怎么做。 I will apreciate any help. 我将不胜感激。
Thanks a lot. 非常感谢。
A solution would be to send $disabled field into the form, but initialize it inside your controller: 一种解决方案是将$ disabled字段发送到表单中,但在控制器内部对其进行初始化:
$disabled = $owner == $currentUser ? false : true;
$form = $this->createForm(new YourFormType($disabled), $messageEntity);
In YourFormType.php 在YourFormType.php中
class YourFormType extends AbstractType
{
protected $disabled;
public function __construct($disabled){
$this->disabled = $disabled;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('shortComment', 'text', array(
'label' => 'Short Message',
'disabled' => $this->disabled
));
}
}
I hope this is useful for you. 希望对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.