2017-04-02 4 views
0

エンティティクラスなしでフォームを作成、レンダリング、送信、検証しようとしています。symfonyフォームはトークンフィールドのみを返します

これを行うには、FormBuilderInterfaceを使用してFormTypeクラスを作成しました。しかし、私が小枝テンプレートでフォームをレンダリングしようとしているとき、私はいつもトークン入力だけでフォームを取得しますが、他のフィールドは取得しません。

私のコードは以下の通りです:

タイプの定義:

<?php 

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 

use Symfony\Component\Form\Extension\Core\Type\EmailType; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 

use Symfony\Component\Validator\Constraints\Email; 
use Symfony\Component\Validator\Constraints\NotBlank; 
use Symfony\Component\Validator\Constraints\Length; 

class VendorLeadType extends AbstractType{ 

    /** 
    * @param FormBilderInterface $builder 
    * @param array $options 
    */ 
    public function buidForm(FormBuilderInterface $builder, array $options){ 

     $builder 
      ->add('email', EmailType::class, [ 
       'constraints' => [ 
        new Email(), 
        new Length(['max'=>'100']) 
       ] 
      ]) 
      ->add('name', TextType::class, [ 
       'constraints' => [ 
        new NotBlank(), 
        new Length(['max'=>'100']) 
       ] 
      ]) 
      ->add('phone', TextType::class, [ 
       'constraints' => [ 
        new NotBlank(), 
        new Length(['max'=>'100']) 
       ] 
      ]) 
     ; 
    } 

} 

コントローラー:

<?php 

namespace AppBundle\Controller; 

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

use AppBundle\Form\VendorLeadType; 

class DefaultController extends Controller 
{ 
    /** 
    * @Route("/", name="homepage") 
    */ 
    public function indexAction(Request $request) 
    { 
     $form = $this->createForm(VendorLeadType::class); 
     return $this->render('index.html.twig', [ 
      'form' => $form->createView() 
     ]); 
    } 

} 

小枝テンプレート

{% extends 'base.html.twig' %} 

{% block body %} 
{{ form(form) }} 
{% endblock %} 

出力HTML

<form name="vendor_lead" method="post"> 
    <div id="vendor_lead"> 
     <input type="hidden" id="vendor_lead__token" name="vendor_lead[_token]" value="..."> 
    </div> 
</form> 

私が間違ってやっている任意のアイデア?

答えて

1

まず、VendorLeadTypeスクリプトに入力ミスがあります。 `public function buildForm 'のスペルを修正する必要があります。

フォーム変数は、あなたのコントローラに来てもらうために、あなたが任意のフォーム変数は、あなたのパラメータに'mapped' => false,を追加することによって、エンティティにマッピングすることを期待しないようにsymfonyに伝える必要があります:

$builder 
     ->add('email', EmailType::class, [ 
      'mapped' => false, 
      'constraints' => [ 
       new Email(), 
       new Length(['max'=>'100']) 
      ] 
     ]) 
     ->add('name', TextType::class, [ 
      'mapped' => false, 
      'constraints' => [ 
       new NotBlank(), 
       new Length(['max'=>'100']) 
      ] 
     ]) 
     ->add('phone', TextType::class, [ 
      'mapped' => false, 
      'constraints' => [ 
       new NotBlank(), 
       new Length(['max'=>'100']) 
      ] 
     ]) 
    ; 
+0

私が試しましたあなたのアイデアは、私は働いていません... – Andrew

+0

申し訳ありませんが、私はより慎重にあなたの質問を読んだ必要があります。私はあなたのコントローラに通ってフォーム変数を取得していないと仮定しました。問題はVendorLeadTypeスクリプトにタイプミスがあることです。 'public function buildForm'のスペルを修正する必要があります。 – ehymel

+0

Oh No.私は問題を解決するために3時間を費やしました...そして理由は構文です!あなたの助けを借りて、それは正しかったです。あなたの答えに修正してください。それを解決策としてマークします。どうもありがとうございました! – Andrew

関連する問題