更改名称形式SF3

时间:2016-09-04 04:00:57

标签: php forms symfony

我搜索了更改SF3生成的表单输入字段的名称,但似乎无法正常工作。

我目前在模板上有两个表单(注册/连接),当我提交两个表单时,两者都会被提交。

我已经读过,我们可以通过名称来处理表单,但是同名的形式"形式",我无法改变这一点,我是阅读SF网站上的文档,但似乎有些不对劲。

这里的代码为1(第二个类似)

<?php 
use AppBundle\Entity\Connection;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

public function getFormConnectionAction( Request $request ) {
        $connection = new Connection();

        $form = $this->createFormBuilder( $connection )
                     ->setAttribute( 'name', 'connection' )
                     ->add( 'pseudo', TextType::class )
                     ->add( 'password', PasswordType::class )
                     ->add( 'send_connection_form', SubmitType::class, array( 'label' => 'Se connecter', ) )
                     ->getForm();

        //Keep the original clean
        $formCloned = clone $form;

        //Handling the request of submit
        $formCloned->handleRequest( $request );

        if ( $formCloned->isSubmitted() && $formCloned->isValid() ) {
            dump( $form->getName() ); //returning name form
        }

        return $this->render( 'forms/connection.html.twig', array( 'form' => $form->createView() ) );
    }

有什么想法吗?保持寻求,但严重的是我没有看到我的错误!

1 个答案:

答案 0 :(得分:0)

好吧,我终于找到了它的解决方案,也许不是更好的解决方案(如果你能评论一下)

我们在这里,我正在定义一些&#34; FormType&#34;像这样:

<?php
namespace AppBundle\Form;

use AppBundle\Entity\Connection;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ConnectionFormType extends AbstractType
{
    public function buildForm( FormBuilderInterface $builder, array $options ) {
        $builder
            ->add( 'pseudo', TextType::class )
            ->add( 'password', PasswordType::class )
            ->add( 'send_connection_form', SubmitType::class, array( 'label' => 'Se connecter', ) );
    }

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

    public function configureOptions( OptionsResolver $resolver ) {
        $resolver->setDefaults([
            'data_class' => Connection::class //My entity for the DB
        ]);
    }
}

我在控制器中通过它调用它:

<?php
namespace AppBundle\Controller;

use AppBundle\Form\ConnectionFormType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class ConnectionController extends Controller {
    /**
     * Get the form connection over all the controllers
     *
     * @return \Symfony\Component\Form\FormInterface
     */
    public function getFormConnectionAction( Request $request ) {
        $form = $this->createForm(ConnectionFormType::class);

        //Keep the original clean
        $formCloned = clone $form;

        //Handling the request of submit
        //$formCloned->handleRequest( $request );
        if($request->isMethod('POST') && $request->request->has('connection_form'))
        {
            $formCloned->submit($request->request->get('connection_form'));
        }

        if ( $formCloned->isSubmitted() && $formCloned->isValid() ) {
            //dump( $formCloned->all()); //returning name
        }

        return $this->render( 'forms/connection.html.twig', array( 'form' => $form->createView() ) );
    }

}

对此有何评论?好吗 ?可以改进吗?还要别的吗 ?