2013-02-17 2 views
1

선택 상자를 사용하여 양식을 작성하고 있습니다. 이 상자는 국가간에 지연되는 비늘을 나타냅니다. 그런 다음Symfony 2를 사용하여 중첩 된 양식의 옵션 푸시

$form = $this->createForm(new formType, $entity, array(

    // Getting the service  
    'myScales'  => $this->get('myBnd.scales'), 

    // Getting user's scale preference 
    'scalesLocale' => $this->get('security.context')->getToken()->getUser()->getScaleLng(), 

    )); 

:

컨트롤러 코드 :

그러므로, 나는 사용자의 preferences.This는 첫 번째 수준 양식을 잘 작동에 따라 선택 상자를 공급하기 위해 사용자 정의 값을 생성하는 서비스를 만들어 나는 사용자 정의 된 select를 표시하기 위해 formType에서 필요한 것만 얻었습니다.

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

    $scaleSelect = array();   
    // Here is a custom code using $options['myScales'] and $options[scalesLocale'] 
    // This builds the relevant $scaleSelect 

    // .... 

$builder 
     ->add('scale', 'choice', array(
      'choices' => $scaleSelect 
     )) 
     ->add('subscale', 'collection', array(
      'type' => new subType, 
      'prototype' => true, 
      'allow_add' => true, 
      'allow_delete' => true, 
     )) 

그런 다음 중첩 된 subType을 정의해야합니다. 또한 사용자 정의 된 선택 상자가 필요합니다. (동일한) 적절한 선택 상자를 생성하려면 어떻게 변수 $scaleSelect을 보낼 수 있습니까? 내 마음을 교차

답변

0

빠른 옵션 : 귀하의 하위 유형 클래스 다음

 ->add('subscale', 'collection', array(
      'type' => new Subtype($scaleSelect), 
      'prototype' => true, 
      'allow_add' => true, 
      'allow_delete' => true, 
     )) 

: 당신은 $ this-> 선택과 buildform에 전달 선택에 액세스 할 수 후

 function __construct($choices) { 
     $this->choices = $choices; 
    } 

. 도움이 되길 바랍니다.

+0

하위 유형이 문자열 (-> add ('하위 범주', '컬렉션', [유형 '=>'mySubType ', ...]))로 전달되는 경우 어떻게해야합니까? 나는. 사용자 정의 양식 유형을 서비스로 전환 할 수 있습니다 (http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html 참조). 이 경우 우리는 생성자에게 아무 것도 전달할 수 없습니다. –

관련 문제