pregmatch pregmatch - 1 year ago 144
PHP Question

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")) {

if ($form->isValid()){
//do stuf
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


array(1) {
object(Symfony\Component\Form\FormError)#839 (4) {
string(26) "Username is already in use"
string(26) "Username is already in use"
array(0) {

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?


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

Answer Source

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) }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download