Symfony2 invalid form without errors

前端 未结 11 734
孤独总比滥情好
孤独总比滥情好 2020-12-01 01:46

I\'ve got a problem with a Symfony2 generated CRUD form. (With MongoDB Documents, but I do not think that this is related)

In my controller\'s createAction() method,

相关标签:
11条回答
  • 2020-12-01 02:26

    Update for Symfony 2.6

    So depending on you Symfony2 version:

    symfony2.3

    die($form->getErrorsAsString());
    

    As of symfony2.5, the getErrorsAsString() function is deprecated (will be removed in Symfony3) and you should use the following method:

    die((string) $form->getErrors());     // Main errors
    die((string) $form->getErrors(true)); // Main and child errors
    

    As of symfony2.6, you can also use the dump (dev environment) function if you have activated the DebugBundle:

    dump((string) $form->getErrors());     // Main errors
    dump((string) $form->getErrors(true)); // Main and child errors
    
    0 讨论(0)
  • 2020-12-01 02:27

    I came across this error and found that I was forgetting to "handle" the request. Make sure you have that around...

    public function editAction(Request $request)
    {
        $form = $this->createForm(new CustomType(),$dataObject);
        /**  This next line is the one I'm talking about... */
        $form->handleRequest($request);
        if ($request->getMethod() == "POST") {
            if ($form->isValid()) {
            ...
    
    0 讨论(0)
  • 2020-12-01 02:28

    From Symfony 3 onwards as per documentation you should do use the new implementation:

    $errors = (string) $form->getErrors(true, false);

    This will return all errors as one string.

    0 讨论(0)
  • 2020-12-01 02:37

    It appears as you have a validation problem. The form is not validating on submitting. I am going to assume you are using Annotations for your validation. Make sure you have this at the top of the entity.

    use Symfony\Component\Validator\Constraints as Assert;
    

    and also this above each property

    /**      
     * @Assert\NotBlank()      
     */
    

    The NotBlank() can be changed to any constraint to fit your needs.

    More information on validation can be found at: http://symfony.com/doc/current/book/validation.html

    More information on Assert constraints can be found at: http://symfony.com/doc/current/book/validation.html#constraints

    0 讨论(0)
  • 2020-12-01 02:38

    For Symfony (>= 3.2 - 4), you can use :

    foreach($form->getErrors(true, false) as $er) {
        print_r($er->__toString());
    }
    

    to see the errors obviously.

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