1

I'm trying to make a search form without an entity.

Controller:

public function SearchFormAction() {
    $collectionConstraint = new Collection(array(
        'size' => new MinLength(3),
    ));

    $searchform = $this->createFormBuilder(null, array(
        'validation_constraint' => $collectionConstraint,
    ))
            ->add('min_range')
            ->add('max_range')
            ->add('bedrooms')
            ->add('bathrooms')
            ->add('size')
            ->add('user')
        ->getForm()
    ;

    return $this->render("RealBundle:User:search.html.twig", array(
                'searchform'  => $searchform->createView(),
            ));
}

View:

<div id="dialog" title="Advanced Search">
<form action="{{ path('searchresults') }}" method="post" {{ form_enctype(searchform) }} id="frmSearch">
<fieldset>
        <h3>Properties</h3>
        <div class="form-search-item">
    {{ form_label(searchform.min_range, 'Price Range') }} {{ form_widget(searchform.min_range) }} to {{ form_widget(searchform.max_range) }}
    {{ form_widget(searchform.min_range) }}
         </div>
         <div class="form-search-item">
            {{ form_label(searchform.bedrooms, 'Bedrooms') }}: {{ form_widget(searchform.bedrooms) }}
         </div>
         <div class="form-search-item">
            {{ form_label(searchform.bedrooms, 'Bathrooms') }}: {{ form_widget(searchform.bathrooms) }}
         </div>
         <div class="form-search-item">
            {{ form_label(searchform.bedrooms, 'Size') }}: {{ form_widget(searchform.size) }}
         </div>
        <h3>User</h3>
        <div class="form-search-item">
            {{ form_label(searchform.user, 'User') }}: {{ form_widget(searchform.user) }}
         </div>
        {{ form_rest(searchform) }}
        <input type="submit" value="Search">
</fieldset>
</form>

I've try with another validations like MinLength, MaxLenght, Type and nothing works for me, what am I doing wrong? I want to validate, range, bedrooms, bathrooms, size as integers, and a minLenght for user.

Tnx and sorry for my english.

1 Answer 1

2

Your validation seems to be working in my test. But you're missing the error messages in the template.

You need

{{ form_errors(form) }}

to render global errors, and then for each field you can display its errors, eg

{{ form_errors(form.size) }}

Then as if by magic you should see your error messages. Although not having seen your controller I can't be sure you're binding and calling isValid.

If you're still having problems then please post your controller too.

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

3 Comments

Tnx for answering, so the validation it's made afeter sending the form to the controller and if it's not valid redirect to the same page showing the errors. There's anyway to validate before submiting the form? Tnx a lot, and sorry for my english again, I'm starting with symfony2.
Your english is fine :) Symfony will generate html5 validation rules for some validators, for example not allowing a blank field. This means if the user didn't fill in the required field and hits submit the browser will alert them and prevent the form from submitting. I don't know if html5 validation can handle anything more complicated than blank fields. There's a brief section in the forms documentation: symfony.com/doc/current/book/forms.html
You're welcome. If you consider your question answered then would you mind marking it so? Thanks

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.