pregmatch
pregmatch

Reputation: 2647

Symfony2 form errors not showing, UniqueEntity error message not shown next to the corresponding field

I am having problems displaying form errors on registration page.

public function createAction {

    if ($request->isMethod("POST")) {

        $form->bind($request);
        if ($form->isValid()){
           //do stuf
        }
        else{
           return array("form"=>$form->createView(), "companies"=> urlencode(json_encode($data)));
        }
    }

Now on my view I have :

{{ form_row(form.user.username) }}
{{ form_errors(form.user.username) }}

I am getting error if username is already in use. This is var_dump of

var_dump($form->getErrors());

array(1) {
  [0]=>
  object(Symfony\Component\Form\FormError)#839 (4) {
    ["message":"Symfony\Component\Form\FormError":private]=>
    string(26) "Username is already in use"
    ["messageTemplate":protected]=>
    string(26) "Username is already in use"
    ["messageParameters":protected]=>
    array(0) {
    }
    ["messagePluralization":protected]=>
    NULL
  }
}

No errors are shown here: {{ form_errors(form.user.username) }}

In my user entity I have:

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @UniqueEntity(
 *     fields={"username"},
 *     message="Your E-Mail adress has already been registered"
 * )
 */

class User implements AdvancedUserInterface, \Serializable

I have also 'error_bubbling' => true on all my inputs in for builder.

I have also try $errors = $this->get('validator')->validate( $registration->getUser() ); And then pass 'errors' => $errors to view but message is not show at corresponding input.

Am I doing this wrong way?

UPDATE

When I put {{ form_errors(form) }} error message is shown.

Upvotes: 1

Views: 1875

Answers (1)

Igor Pantović
Igor Pantović

Reputation: 9246

You turned on error_bubbling and therefore all error messages from child forms are bubbled up to root form.

If you want to show form errors for each field specifically, set it to false.

So to sum it up:

if error_bubbling = true then you show all errors like this:

{{ form_errors(form) }}

if error_bubbling = false then you show errors for each individual form separately:

{{ form_errors(form.user.username) }}

Upvotes: 1

Related Questions