4

A form has been created in Symfony2 using createFormBuilder, it does not need an entity and is only used for searching. The name of the form and the input name need to be changed.

    $form = $this->createFormBuilder(null, array(
                'action' => $this->generateUrl('route'),
                'method' => 'GET',
        'csrf_protection' => false,
        'attr' => array('name' => 'search'),
            ))
            ->add('query', 'text', array(
                'required' => false,
            ))
            ->getForm();

The rendered HTML is as follows:

<form class="" method="GET" action="route" name="form">
<div class="errors"></div>
<div id="form" name="search">
<div>
<label for="form_query">Query</label>
<input id="form_query" type="text" value="king" name="form[query]">
</div>
</div>
<p>
</form>

How can the form name be changed? Currently it is form, which means the div id is also form, could it be changed to search?

Secondly the input should have a name as query not form[query]?

I have had a look at the docs about working with a form without an entity but I can not see how to change these properties.

The end result should be:

<form class="" method="GET" action="route" name="search">
<div class="errors"></div>
<div id="search">
<div>
<label for="search_query">Query</label>
<input id="search_query" type="text" value="king" name="query">
</div>
</div>
<p>
</form>
3
  • I don't have an example handy but try: $this->container->get('form.factory')->createNamedBuilder('search','form', $data, $options); Commented Aug 18, 2014 at 12:44
  • @Cerad if you pass "search" as first argument, all fields in the form will be named as search[...] Commented Aug 18, 2014 at 12:50
  • @Dmitry - Correct. My suggestion only changes the form name. You cannot get around having the search_query input being named search[query]. At least not without getting into complex form event handling or just doing it by hand. Commented Aug 18, 2014 at 12:57

1 Answer 1

7

You should call createNamedBuilder. It allows you to set custom form name in the first argument.

Example:

    // public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array())
    $form = $this->get('form.factory')->createNamedBuilder('', 'form', array(), array(
        'action' => $this->generateUrl('route'),
        'method' => 'GET',
        'csrf_protection' => false,
        'attr' => array('name' => 'search'),
    ))->add('query', 'text', array(
        'required' => false,
    ))->getForm();

For more useful methods, please see: \Symfony\Component\Form\FormFactory

Sign up to request clarification or add additional context in comments.

1 Comment

createBuilder($type = 'form', $data = null, array $options = array())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.