2014-05-19 4 views
0

나는 문서의이 부분에보고했다 : 내 경우를 제외하고Symfony2 정적 인 형태의 컬렉션 레이블

http://symfony.com/doc/current/cookbook/form/form_collections.html

tagquestion입니다 각 질문에 대해 고유 한 라벨을 가지고있다.

컬렉션 양식에 고유 한 레이블을 만들려면 어떻게해야합니까?

는 QuestionType :

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class QuestionType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('Answer', 'choice' array(
     'choices' => array(
        '' => 'select one', 
        'yes', 
        'no') 
     )); 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'Acme\AcmeBundle\Entity\Question\Question', 
     )); 
    } 

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

문진 컬렉션 :

class BriefQuestionaireType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('Questions', 'collection', array(
      'type' => new QuestionType() 
      ) 
     ); 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'Acme\AcmeBundle\Entity\Question\Questionaire', 
     )); 
    } 

    public function getName() 
    { 
     return 'briefquestionaire'; 
    } 
} 
나는 같은 것을 할 수 있어야합니다

:

$builder->add('Questions', 'collection', array(
       'label' => 'Q1:Have you ever...?', //I'd like to use unique but static questions so that I can reuse the questions again later.` 
       'type' => new QuestionType() 
       ) 
      ); 


$builder->add('Questions', 'collection', array(
       'label' => 'Q2:Have you also...?', //I'd like to use unique but static questions so that I can reuse the questions again later.` 
       'type' => new QuestionType() 
       ) 
      ); 

은 위의 페이지를 덮어 그러나 이전 레이블. 레이블 Q2:Have you also...? 만 나타납니다.

바라기를 바라지 만 이제는 더 명확합니다. 예/선택의 여지가없는 고유 레이블 (정적 인 질문)을 원했을 때의 의미였습니다. 난 당신이 달성하고자하는 일에 대해 옳다 경우

+1

컬렉션의 고유 라벨은 무엇을 의미합니까? 컬렉션의 첫 번째 요소가 '첫 번째 태그'라는 레이블을 얻는 것처럼 두 번째 요소는 'some other tag'라는 레이블을 얻습니다. – nifr

+0

자세한 내용은 내 질문을 편집했습니다. – Tek

답변

2

단일 수집 필드 만 있으면됩니다. 양식에 바인딩하는 데이터의 "questions"배열은 표시되는 QuestionType 필드의 수를 결정합니다. 예컨대 ...

다음
// Acme/DemoBundle/Controller/DefaultController.php 

// ... 

$data = array(
    'Questions' => array(
     array('Answer' => 'First Answer'), 
     array('Answer' => 'Second Answer'), 
     array('Answer' => 'Third Answer') 
    ); 
); 

$form = $this->createForm(new BriefQuestionaireType(), $data); 

// ... 

문제가 어떻게 그들 모두에 대한 옵션이 동일 할 때, 각 질문 필드에 대한 고유 레이블을 표시합니까?

다른 날이 문제가 발생하여이를 해결했습니다.

// Acme/DemoBundle/Form/LabelGenerator.php  

class LabelGenerator{ 

    private $labels; 

    public function __construct(array $labels){ 
     $this->labels = $labels; 
    }   

    public function __toString(){ 
     $keyValue = each($this->labels); 
     return $keyValue['value']; 
    } 

} 

// Acme/DemoBundle/Form/Type/BriefQuestionaireType.php 

// ... 

public function buildForm(FormBuilderInterface $builder, array $options) { 

    $labelGenerator = new LabelGenerator(array(
     'Q1: What is the first question?', 
     'Q2: What is the second question?', 
     'Q3: What is the third question?' 
    )); 

    $builder->add('Questions', 'collection', array(
     'type' => new QuestionType(), 
     'options' => array(
      'label' => $labelGenerator  
     ) 
    )); 

} 

// ... 

양식 테마가 Question 레이블을 렌더링 할 때마다 LabelGenerator는 배열의 다음 값을 반환합니다.

setDefaultOptions()를 사용하여 BriefQuestionaireType 양식에 "question_labels"옵션을 추가하는 것이 좋습니다. 그런 다음이를 컬렉션에 전달할 수 있습니다.

// Acme/DemoBundle/Controller/DefaultController.php 

// ... 

$form = $this->createForm(new BriefQuestionaireType(), $data, array(
    'question_labels' => $labels 
)); 

// ... 

// Acme/DemoBundle/Form/Type/BriefQuestionaireType.php 

// ... 

public function setDefaultOptions(OptionsResolverInterface $resolver) { 

    $resolver->setRequired(array('question_labels')); 

} 

public function buildForm(FormBuilderInterface $builder, array $options) { 

    $labelGenerator = new LabelGenerator($options['question_labels']); 

    $builder->add('Questions', 'collection', array(
     'type' => new QuestionType(), 
     'options' => array(
      'label' => $labelGenerator  
     ) 
    )); 

} 

// ... 

또는 Question 엔터티에서 레이블을 가져 오려면 다음과 같이하십시오.

// Acme/DemoBundle/Form/Type/BriefQuestionaireType.php 

// ... 

public function buildForm(FormBuilderInterface $builder, array $options) { 

    $labels = array(); 
    foreach($builder->getData()->getQuestions() as $question){ 
     $labels[] = $question->getLabel(); 
    } 

    $labelGenerator = new LabelGenerator($labels); 

    $builder->add('questions', 'collection', array(
     'type' => new QuestionType(), 
     'options' => array(
      'label' => $labelGenerator  
     ) 
    )); 

} 

// ... 

편집 : 질문을 적절한 숫자의 질문 항목으로 미리 채워야합니다. 그렇지 않으면 빈 채로 남을 것입니다.

0

나는 일주일 전에 비슷한 질문에 대답했다 :

기본적으로 How to pass entity atribute value to form Symfony2?

이 솔루션은 Form에 데이터를 전달하고 그것의 구축 FormEvent들에 의존 필드 dinamically.

+0

아주 가깝습니다! 실제로 컬렉션에서 정적 레이블을 사용하여 여러 위치에서 반복적으로 양식을 재사용 할 수 있습니다. 추가 정보 btw에 대한 내 질문을 편집했습니다. – Tek

관련 문제