2012-07-26 1 views
0

양식을 렌더링 할 때 컬렉션의 첫 번째 항목에 대해서만 읽기 전용 옵션을 설정하려면 어떻게해야합니까?Symfony 양식 컬렉션 첫 번째 항목에 대한 읽기 전용

내 간단한 모델 :

내 모델
class Main 
{ 
    public $others; 
} 

class Other 
{ 
    public $field1; 

    public $field2; 
} 

간단한 양식 유형 : 수동으로 렌더링 할 때이 조건을 만들 수 있습니다

//... 
public function indexAction($id) 
{ 
    $main = new Main(); 

      $other1 = new Other(); 
    $other1->field1 = 'a'; 
    $other1->field2 = 'b'; 
    $main->others[] = $other; 

      $other2 = new Other(); 
    $other2->field1 = 'c'; 
    $other2->field1 = 'd'; 
    $main->others[] = $other; 

    $form = $this->createForm(new MainType(), $main); 

    //...isValid, persist, flush... 
} 
//... 

class MainType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('others', 'collection', array(
       'type' => new OtherType(), 
       'allow_delete' => true, 
       'allow_add' => true, 
       'by_reference' => false, 
      )) 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'App\MyBundle\Entity\Main', 
     )); 
    } 

    public function getName() 
    { 
     return 'maintype'; 
    } 
} 

class OtherType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('field1') 
      ->add('field2') 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'App\MyBundle\Entity\Other', 
     )); 
    } 

    public function getName() 
    { 
     return 'othertype'; 
    } 
} 

그리고 간단한 액션 메소드 내 컨트롤러 양식,하지만 가능한 경우 양식 코드에서 이러한 제한을 입력하려면 알고 싶습니다.

답변

2

현재 컬렉션의 행에 다른 옵션이있을 수 없습니다. 귀중한 도움이된다고 생각하시면 issue tracker에 기능 요청을 작성하겠습니다.

+0

+1 호기심 @Bernhard. 이것도 Entity 필드 유형의 경우입니까? 엔티티 필드 유형의 첫 번째 항목에 대해 읽기 전용을 설정하려는 경우 가능합니까? – Mick

+0

필드를 드롭 다운, 체크 박스 또는 라디오 단추로 렌더링할지 여부에 따라 다릅니다. –