0

I'm trying to create, render, submit and validate form without entity class.

To do that, I've created FormType class using FormBuilderInterface. But when i'm trying to render form in twig template, I always get only form with token input, but no other fields.

My code is below:

Type definition:

<?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'])
                ]
            ])
        ;
    }

}

Controller:

<?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()
        ]);
    }

}

Twig template

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

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

Output 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>

Any idea what i'm doing wrong?

Andrew
  • 671
  • 2
  • 8
  • 21

1 Answers1

1

First, you have a typo in your VendorLeadType script. You need to fix the spelling of `public function buildForm'.

To get the form variables to come to your controller, you'll need to tell symfony to not expect any form variables to map to an entity by adding 'mapped' => false, to your parameters:

    $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'])
            ]
        ])
    ;
ehymel
  • 1,360
  • 2
  • 15
  • 24
  • Sorry, I should have read your question more carefully. I assumed you were not getting form variables to come through to your controller. The problem is that you have a typo in your VendorLeadType script. You need to fix the spelling of `public function buildForm`. – ehymel Apr 03 '17 at 01:09
  • Oh no. I've spent for 3 hours to fix the problem... and the reason is in syntax! With your help it's working correct. Please, fix it in your answer and I'll mark it as a solution. Thank you very much! – Andrew Apr 03 '17 at 08:50
  • Thanks, I added to my answer. – ehymel Apr 03 '17 at 15:14