简体   繁体   中英

symfony2 unwanted field displayed in form?

Using symfony 2.3 I'm trying to use a dumb form based on class form (without entity) like this:

SearchType.php

<?php

namespace Floarc\ParkingBundle\Form;

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

class SearchType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('search', 'text');
    }

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

And this is what I got in my controller:

   /**
     * @Route("/search", name="_farc_search")
     * @Template()
     */
    public function searchAction(Request $request)
    {
        $form = $this->createForm(new SearchType());
        $form->handleRequest($request);


        return array('form' => $form->createView());
    }

And in my view

{{ form(form) }}

Simple isn't it...

But when I display this page I get 2 fields displayed instead of only one!

Here is the code of the form:

<form name="search" method="post" action="">
    <input type="search" id="search" name="search" required="required">
    <div>
        <label for="search_search" class="required">Search</label>
        <input type="text" id="search_search" name="search[search]" required="required">
    </div>
    <input type="hidden" id="search__token" name="search[_token]" value="oMyq2WORCXyD97WKLb309F0pR1NpDkvVyi8FgqilUzo">
</form>

The field I've added has been named with the id="search_search" and a correct associated label, and of course I've got an input for the token.

But I do not understand where the input id="search" came from? Furthermore this input has no label?

Any ideas?

Maybe getName() that returns "search" conflicts with built-in search form type.

Try changing it to return something else...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM