1

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?

7
  • 2
    It's more likely that you forgot to wrap evething with <form>, right? Commented Nov 22, 2013 at 15:32
  • if I place form tag in twig then It click and validate missing fields but still does not call $form->handleRequest($request); Commented Nov 22, 2013 at 15:37
  • You are referring to HTML5 validation? @Ahmed Siouani explained in his answer what you need to do "by the book" :) Commented Nov 22, 2013 at 15:39
  • no I am simply talking about form validation check $form->isValid()) it does not call. Commented Nov 22, 2013 at 15:41
  • But, that doesn't make sense :) My first guess would be that you are missing CSRF token field - that is why isValid() is failing. Can you die(print_r($form->getErrors(), TRUE));? Commented Nov 22, 2013 at 15:44

1 Answer 1

4

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}) }}
Sign up to request clarification or add additional context in comments.

7 Comments

It added the whole form which is not needed here. I want to customise form fields manually in twig rather than load the complete form.
form_start and form_end don't show the whole form. they only show the start and the end <form> tags. You can then customize your form fields manually between these two tags.
Agreed. @AliHassan What are you referring to is form_widget(form) statement.
{{ form_start(form) }} <h1>Company Information</h1> <table style="width:50%"> <tr> <td style="width:50%">Company Name</td><td style="width:.1">{{ form_row(form.CompanyName) }}</td> </tr> <tr> <td style="width:50%">Address Line 1</td><td style="width:.1">{{ form_row(form.Address1) }}</td> </tr> <tr> <td style="width:50%">Address Line 2</td><td style="width:.1">{{ form_row(form.Address2) }}</td> </tr> <tr> <td> {{form_widget(form.Continue_to_Step_2)}} </td> </tr> </table> I added this and now my remaining fields are also shown on screen.:(
Hey, read the documentation of form_end I sent you ! {# don't render unrendered fields #} {{ form_end(form, {'render_rest': false}) }} You've then to pass false to the render_rest option of form_end. I updated my answer to make it clearer on this point :)
|

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.