Silex:如何在Form中创建循环

时间:2017-04-24 11:33:37

标签: php forms loops foreach silex

以下代码行给我一个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')
        ])
    }

但是,我不知道如何组合这两个部分,因为$ form变量是在一行中定义的。与';'在末尾。如何将这个$ form分成几部分,以便我可以循环访问我的用户并为每个用户添加一个复选框?

1 个答案:

答案 0 :(得分:0)

执行->getForm();后,表单会“生成”,因此如果您想在生成之前添加任何字段,则应按->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();