5

I'm new to symfony and twig. I want to build my html forms using BootStrap CSS Framework. So my form looks like this in twig file:

<form action="{{ path('register')}}" class="form-horizontal span10 offset7" id="frmRegistration" method="POST"  {{ form_enctype(form) }} novalidate>
<fieldset>
    {{ form_widget(form._token) }}
    <div class="control-group">
        {{ form_label(form.userName, null, {'label_attr': {'class':'control-label'}})}}
        <div class="controls">
            {{ form_widget(form.userName, {'attr': {'data-path': path('ajax_user_exists') } }) }}
        </div>
    </div>
    <div class="control-group">
        {{ form_label(form.password.first, null, {'label_attr': {'class':'control-label'}})}}
        <div class="controls">
            {{ form_widget(form.password.first) }}
        </div>
    </div>
    <div class="control-group">
        {{ form_label(form.password.second, null, {'label_attr': {'class':'control-label'}})}}
        <div class="controls">
            {{ form_widget(form.password.second) }}
        </div>
    </div>

    <div class="control-group">            
        <div class="controls">
            <input id="register_submit" type="submit" value="تاييد" class="btn btn-primary" />
        </div>
    </div>     
</fieldset>

Now I want to show validation error messages exactly in front of each input. My problem is that how can I do this using twig? If I use {{ form_row(form.firstName) }} it will generate the label and input. But I cant wrap them inside the Bootstrap form structure. Any help is appreciated in advance.

UPDATE

Sorry for not being precise when reading symfony documentation. I found the solution. Using {{ form_errors(form.firstName) }} solved the problem.

2
  • Did you mean {{ form_error(form.firstName) }}? form_error() is different from form_errors(). Commented Nov 6, 2013 at 20:19
  • 2
    You should read this : symfony.com/doc/current/cookbook/form/…, there is a way to personalize the form rendering, so you could use form_row to display your fields the way you want... Commented Nov 6, 2013 at 21:44

1 Answer 1

3

This is still showing up as unanswered and thought it would be helpful if the answer was added at the end so this could be closed.

To add an error message on a form row, use:

{{ form_errors(form.firstName) }}

You will need to add the widget with this and the label if you want a label to show. Full rendering for each row requires all three:

{{ form_label(form.firstName) }}
{{ form_errors(form.firstName) }}
{{ form_widget(form.firstName) }}
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting an error saying no function found form_errors ? I am using spring java

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.