2014-12-17 3 views
0

저는 Symfony2에서 새롭고 양식 유형에 문제가 있습니다.symfony에서 조건에 따라 텍스트 상자 사용 안 함

데이터베이스에 '메시지'테이블이 있으며 모든 메시지에는 ID, 메시지 및 소유자가 있습니다.

그런 다음이 메시지를 양식 안에 표시해야합니다. 그래서 나는이 작업을 수행 :이 텍스트 상자를 사용하지 않을 때

->add('shortComment', 'text', array(
      'label' => 'Short Message', 
      'data' => $this->_predefinedMessage->getShortComment())) 

그래서, 나를 위해 문제가있는 경우 :

owner != currentUser 

내가보다 선행하는 방법을 모르겠어요. 나는 어떤 도움을 apreciate 것입니다.

고마워요.

+0

http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#customizing-your-form-based-on-the-underlying-data –

+0

[양식 기반을 동적으로 생성하는 방법 사용자 데이터] (http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#how-to-dynamically-generate-forms-based-on-user-data) – Matteo

답변

1

해결책은 양식에 $ 장애인 필드를 보낼 수 있지만 컨트롤러 내부를 초기화합니다 :

$disabled = $owner == $currentUser ? false : true; 

$form = $this->createForm(new YourFormType($disabled), $messageEntity); 

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 
     ));  
    } 

} 

에서 나는이 당신을 위해 유용 바랍니다.

+0

알 겠어! 정말 고마워! –

관련 문제