Get all errors along with fields the error is connected to

前端 未结 7 1379
予麋鹿
予麋鹿 2021-01-03 06:43

I\'m using Symfony2 forms to validate POST and PUT requests to an API. The form handles binding the request data to the underlying entity and then validating the entity. Eve

相关标签:
7条回答
  • 2021-01-03 07:27

    and for symfony >= 2.2

    private function getErrorMessages(\Symfony\Component\Form\Form $form) {
        $errors = array();
        foreach ($form->getErrors() as $key => $error) {
            $template = $error->getMessageTemplate();
            $parameters = $error->getMessageParameters();
    
            foreach ($parameters as $var => $value) {
                $template = str_replace($var, $value, $template);
            }
    
            $errors[$key] = $template;
        }
        if ($form->count()) {
            foreach ($form as $child) {
                if (!$child->isValid()) {
                    $errors[$child->getName()] = $this->getErrorMessages($child);
                }
            }
        }
        return $errors;
    }
    
    0 讨论(0)
提交回复
热议问题