我无法提交表格

时间:2016-05-14 00:23:47

标签: php forms symfony symfony-2.6

我想制作多步表单,所以我创建了一个类表单,并在控制器中创建了一个函数,我的问题是我无法提交表单

我尽力而为:

  • $ formTransfertFonds-> isValid()
  • $ formTransfertFonds-> isSubmitted()
  • $这 - >获得('请求') - > isMethod('后&#39)
    public function indexTransfertFondsCcpAction()
    {
        $request = $this->get('request');
        $transfertFonds = new TransfertFonds();
        $step = 1;
        $formTransfertFonds = $this->createForm(new TransfertFondsType($step), $transfertFonds );
        $formTransfertFonds->handleRequest($request);

        if ($formTransfertFonds->isSubmitted())
        if ( $this->get('request')->isMethod('post') ) {
            echo "ok"; }
        }

        return $this->render('@EgovPoste/Ccp/indexTransfertFondsAccountCcp.html.twig',
            array('formTransfertFonds' => $formTransfertFonds->createView(),
                  'step' => $step
            )
        );
    }

我创建一个变量$ step,将其传递给我的表单

class TransfertFondsType extends AbstractType
{
    protected $step;
    public function __construct($step) {$this->step = $step;}
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        switch ($this->step) {
            case 1:
                $builder
                    ->add('toNumCompte')
                    ->add('montant', 'money')
                    ->add('Suivant', 'submit')
                    ->getForm();

                break;
            case 2:
                $builder
                    ->add(
                        'typeTransfert',
                        'choice',
                        array(
                            'choices' => array(
                                'maintenant' => 'Juste maintenant',
                                'unique' => 'Unique',
                                'permanent' => 'Permanent',
                            ),
                            'expanded' => true,
                            'multiple' => false,
                        )
                    )
                    ->add(
                        'optionEmail',
                        'checkbox',
                        array(
                            'label' => 'Recevoir une copie par e-mail de ma confirmation de virement',
                            'data' => false,
                            'required' => false,
                        )
                    )
                    ->add('Retour', 'submit')
                    ->add('Suivant', 'submit');
                break;
            case 3:
                break;
            case 4:
                break;
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Egov\CoreBundle\Entity\TransfertFonds',
            )
        );
    }

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

1 个答案:

答案 0 :(得分:0)

您可以尝试:

if ($formTransfertFonds->isSubmitted() && $formTransfertFonds->isValid()) {
        //Do what you want
    }

要在表格使用中传递变量:

$this->createForm(new TransfertFondsType($step), $transfertFonds, array('attr' => array('step' => $step );

并将其用于:

$options['attr']['step']