2017-04-24 3 views
0

다음 코드 줄은 Silex Form을 가져옵니다. 하나의 텍스트 필드와 하나의 확인란. 텍스트 필드에는 팀 이름이 들어 있습니다. 확인란은 사용자의 사용자 이름을 포함해야하므로 팀에 추가 할 수 있습니다.Silex : 양식에 루프를 만드는 방법

$form = $app['form.factory']->createBuilder(FormType::class) 
     ->add('name', TextType::class, array(
      'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 4,'max' => 64))), 
      'label' => 'Team Name', 
      'required' => 'required', 
      'attr' => array('class' => 'input-field', 'autocomplete' => 'off', 'value' => $team->data()->name), 
      'label_attr' => array('class' => 'label') 
     )) 
     ->add('players', CheckboxType::class, [ 
       'constraints' => array(new Assert\NotBlank()), 
       'label' => $player->username, 
       'attr' => array('class' => 'input-field', 'value' => $player->username), 
       'label_attr' => array('class' => 'label') 
     ]) 
     ->add('submit', SubmitType::class, [ 
      'label' => 'Edit', 
      'attr' => array('class' => 'submit'), 
     ]) 
     ->getForm(); 

나는 다음과 같은 라인을 사용하여 사용자의 내 목록을 얻을 수 있습니다 : 그러나

$user = new User() 
$user->getList(); 

foreach($user->data() as $player){ 
     ->add('players', CheckboxType::class, [ 
       'constraints' => array(new Assert\NotBlank()), 
       'label' => $player->username, 
       'attr' => array('class' => 'input-field', 'value' => $player->username), 
       'label_attr' => array('class' => 'label') 
     ]) 
    } 

을, 나는 $ 양식 변수가 하나 '에 정의되어 있기 때문에,이 두 부분을 결합하는 방법을 모른다 줄 '을'; ' 결국. 어떻게이 $ 폼을 부분으로 나누어 사용자를 반복하고 모든 사용자에 대해 확인란을 추가 할 수 있습니까?

+0

것은 그냥 추가 '$ form-> getForm();을 수행하기 전에 모든'add()'(루프 포함) 메소드를 호출하십시오. – olibiaz

+0

그래서 어떻게 할 것인가에 대한 스크립트를 나에게 줄 수 있습니까? 나는 아직도 그것을 다루는 방법에 의문을 품기 때문에 –

답변

0

당신이 ->getForm();을 실행하면 양식 그래서 코드는 아마 같이해야 당신이 그것을 생성하기 전에 anyfield을 추가하려는 경우 그래서, 당신은 ->getForm();

으로 마무리해야한다 "생성"됩니다 :

// add you "static" fields 
$formBuilder = $app['form.factory']->createBuilder(FormType::class) 
    ->add('name', TextType::class, array(
     'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 4,'max' => 64))), 
     'label' => 'Team Name', 
     'required' => 'required', 
     'attr' => array('class' => 'input-field', 'autocomplete' => 'off', 'value' => $team->data()->name), 
     'label_attr' => array('class' => 'label') 
    )) 
    ->add('players', CheckboxType::class, [ 
      'constraints' => array(new Assert\NotBlank()), 
      'label' => $player->username, 
      'attr' => array('class' => 'input-field', 'value' => $player->username), 
      'label_attr' => array('class' => 'label') 
    ]) 
    ->add('submit', SubmitType::class, [ 
     'label' => 'Edit', 
     'attr' => array('class' => 'submit'), 
    ]); 


$user = new User(); 
$user->getList(); 

// then add your "dynamic" fields 

foreach($user->data() as $player) { 
    $formBuilder->add('players', CheckboxType::class, [ 
      'constraints' => array(new Assert\NotBlank()), 
      'label' => $player->username, 
      'attr' => array('class' => 'input-field', 'value' => $player->username), 
      'label_attr' => array('class' => 'label') 
    ]); 
} 


// then generate your form 
$form = $formBuilder->getForm();