Symfony2: my form returns false from isValid() but empty array for getErrors() from unique constraint condition

前端 未结 6 851
天命终不由人
天命终不由人 2021-02-13 04:02

I have a Customer entity that only has a unique Email field to it. I am trying to edit a customer\'s email and the validation works fine. However I have this in my controller:

相关标签:
6条回答
  • 2021-02-13 04:31

    For debug purposes you can use $form->getErrorsAsString() instead of $form->getErrors() if you use Symfony 2.*

    Quoted from this answer:

    $form->getErrorsAsString() should only be used to debug the form...it will contain the errors of each child elements which is not the case of $form->getErrors().


    UPDATE 1:

    "With more recent Symfony versions, you must use $form->getErrors(true, false); instead. First param corresponds to deep and second to flatten" (see the comment by @Roubi)

    0 讨论(0)
  • 2021-02-13 04:37

    The following solutions works for me:

    $form->getErrors(true)

    0 讨论(0)
  • 2021-02-13 04:38

    Ok, found an answer here:

    Symfony2 invalid form without errors

    It turns out each form child has it's own separate errors. When doing a var_dump of

    $editForm->getChildren()['email']->getErrors()
    

    I get:

    array (size=1)
      0 => 
        object(Symfony\Component\Form\FormError)[531]
          private 'message' => string 'A customer under that email address already exists' (length=50)
          protected 'messageTemplate' => string 'A customer under that email address already exists' (length=50)
          protected 'messageParameters' => 
            array (size=0)
              empty
          protected 'messagePluralization' => null
    

    I am still wondering how to determine that the error is because of a unique conflict without parsing the error message string.

    0 讨论(0)
  • 2021-02-13 04:47

    In Symfony 2.3, you can use this one :

    if ($form->isValid()){
        # Code...
    } else {
        foreach ($form->getIterator() as $key => $child) {
            if ($child instanceof Form) {
                foreach ($child->getErrors() as $error) {
                    $errors[$key] = $error->getMessage();
                }
            }
        }
    }
    

    This will get you an array ($errors) with the errors from the children.

    0 讨论(0)
  • 2021-02-13 04:51

    You can use error_bubbling on each field to bubble the error up to your $form.

    If not, you can also foreach through the errors

    foreach ($children as $child) {
                if ($child->hasErrors()) {
                    $vars = $child->createView()->getVars();
                    $errors = $child->getErrors();
                    foreach ($errors as $error) {
                        $this->allErrors[$vars["name"]][] = $this->convertFormErrorObjToString($error);
                    }
                }
    }
    
    0 讨论(0)
  • 2021-02-13 04:51

    You could try to use the dump function when the form is submited and not valid. I use it like this

    if($form->isSubmited() && $form->isValid()){
       //SAVE TO DATABASE AND DO YOUR STUFF
    }else if($form->isSubmited()){
      //SUBMITED BUT WITH ERRORS
       dump($form->getErrors(true));
       die();
    
    }
    

    Note this is for debugging purposes only, It will show you your form, the data in it and all the errors any field could have. In production mode you should return the error to the view and show them to the user.

    0 讨论(0)
提交回复
热议问题