1

I am trying to create a simple form within Symfony. Not quite sure why this is not grabbing the form variable.

Receiving error:

Variable "form" does not exist src/Thinkfasttoys/MapWatchBundle/Resources/views/Default/createMapPolicy.html.twig at line 30

Controller - DefaultController.php

class DefaultController extends Controller
{
public function policyFormAction()
{
    $form = $this->createFormBuilder()
        ->add('name', 'text')
        ->add('age', 'integer')
        ->add('save', 'submit')
        ->getForm()
        ;

    return $this->render('ThinkfasttoysMapWatchBundle:Default:createMapPolicy.html.twig', array(
        'form' => $form->createView(),
        ));
}

View - createMapPolicy.html.twig

{% block body %}
            <div class="row-fluid">
                <div class="span12">
                    <div class="widget-box">
                        <h4 align="center", padding="10px 0 10px 0">Create a New MAP Policy</h4>

                        {{ form(form) }}

                        <div class="container-1">

                        </div><!-- /.container -->    
                    </div><!-- /.widgetbox -->
                </div>
            </div>
{% endblock %}
3
  • 1
    what symfony version are you using? Commented Jul 28, 2017 at 21:26
  • I notice in your render you specify: ThinkfasttoysMapWatchBundle:Default:createMapPolicy.html.twig but in your error it shows: src/Thinkfasttoys/MapWatchBundle/Resources/views/Default/createMapPolicy.html.twig Is it possible you have two different files? Commented Jul 28, 2017 at 23:35
  • I am using version 2.7 Commented Aug 3, 2017 at 14:20

1 Answer 1

1

In twig you have to display the form like this:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
Sign up to request clarification or add additional context in comments.

3 Comments

tried this previously - produces same variable "form" does not exist error
where are you mapping your route?
Actually, form(form) will work just fine. Your question about route mapping is a good one. It is possible that a different action is calling the template though it seems unlikely.

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.