Ali Hassan
Ali Hassan

Reputation: 607

Symfony Form's button does not post data

I am trying to post data through Symfony form's button but it does not validate form.

Here is my controller file:

public function PurchaseProductAction(Request $request)
    {
$defaultData = array('message' => 'Type your message here');
         $form = $this->createFormBuilder($defaultData)
    ->setMethod('POST')
        ->add('CompanyName', 'text', array(
        'label'=>false    
    ))
    ->add('Address1', 'text', array(
        'label'=>false   
    ))
        ->add('Continue_to_Step_2', 'submit')
        ->getForm();

$form->handleRequest($request);
if ($form->isValid()) 
    {
        // It does not come here
        $data = $form->getData();
        $value = $data['CompanyName'];
            echo $value;
    }
}

Its my twig file: {% block content %}

Company Name{{ form_row(form.CompanyName) }} Address Line 1{{ form_row(form.Address1) }} {{form_widget(form.Continue_to_Step_2)}}

{% endblock %}

Kindly guide me what I am doing wrong due to which my method does not call?

Upvotes: 1

Views: 1158

Answers (1)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

As explained in the Rendering a Form in a Template part of the documentation, you've to include the {{ form_start(form) }} and the {{ form_end(form) }} twig form helpers.

This will generate the appropriate <form> tags according to your form definition.

Also, keep in mind that Support for submit buttons was added in Symfony 2.3. Before that, you had to add buttons to the form's HTML manually.

Update,

form_end should be called with render_rest option set to false if you don't want it to show unrendered fields,

{# don't render unrendered fields #}
{{ form_end(form, {'render_rest': false}) }}

Upvotes: 4

Related Questions