2016-03-15 65 views
1

我有一個註冊表單,其中包含在用戶實體類中驗證的字段。驗證工作正常,但是我不能返回帶有表單錯誤消息的JsonResponse。JsonResponse中沒有顯示任何表單錯誤 - Symfony

我的登記表控制器的方法是這樣的:

/** 
    * @Route("/register", name="register") 
    */ 
    public function registerAction(Request $request) 
    { 
     $user = new User(); 
     $form = $this->createForm(RegistrationType::class, $user); 
     $form->handleRequest($request); 
     $errors = ""; 

     if ($form->isSubmitted()) 
     { 
      if ($form->isValid()) 
      { 
       $password = $this->get('security.password_encoder') 
        ->encodePassword($user, $user->getPlainPassword()); 
       $user->setPassword($password); 

       $user->setIsActive(1); 
       $user->setLastname('none'); 

       $em = $this->getDoctrine()->getManager(); 
       $em->persist($user); 
       $em->flush(); 

       return new JsonResponse(
        array(
         'message' => 'Success! User registered!', 
        ), 200); 
      } 
      else 
      { 
       $errors = ($this->get('validator')->validate($form)); 

       return new JsonResponse(
        array(
         'message' => 'Not registered', 
         'errors' => $errors, 
        ), 400); 
      } 
     } 

     return $this->render(
      'ImmoBundle::Security/register.html.twig', 
      array('form' => $form->createView(), 'errors' => $errors) 
     ); 
    } 

我得到以下JSON響應,當我提交無效數據登記表:

{"message":"Not registered","errors":{}}

其實我在說「錯誤「:{}將包含一些錯誤字段,但它不包含。有誰知道這裏的問題是什麼?

UPD:

我RegistrationType看起來是這樣的:

class RegistrationType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('firstname', TextType::class) 
      ->add('email', EmailType::class) 
      ->add('plainPassword', RepeatedType::class, array(
       'type' => PasswordType::class, 
       'first_options' => array('label' => 'Password'), 
       'second_options' => array('label' => 'Repeat password'), 
       'invalid_message' => "Passwords don't match!", 
      )) 
      ->add('register', SubmitType::class, array('label' => 'Register')); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
       'data_class'  => 'ImmoBundle\Entity\User', 
       'csrf_protection' => true, 
       'csrf_field_name' => '_token', 
       'csrf_token_id'  => 'authenticate', 
      )); 
    } 
} 

UPD2:找到了解決辦法。我需要做的這個迭代,然後調用的getMessage():

$allErrors = ($this->get('validator')->validate($form)); 

foreach ($allErrors as $error) 
{ 
    $errors[] = $error->getMessage(); 
} 

回答

2

表,當你調用$form->handleRequest($request);

要獲得形式錯誤使用getErrors方法

$errors = $form->getErrors(true); // $errors will be Iterator 

轉換錯誤反對驗證消息陣列可以使用代碼從這個迴應 - Handle form errors in controller and pass it to twig

這是exapmle我是如何在過程中的錯誤我的其中一個項目

$response = $this->get('http.response_formatter'); 
if (!$form->isValid()) { 
    $errors = $form->getErrors(true); 
    foreach ($errors as $error) { 
     $response->addError($error->getMessage(), Response::HTTP_BAD_REQUEST); 
    } 

    return $response->jsonResponse(Response::HTTP_BAD_REQUEST); 
} 

這對我很有用。

而且這也可以幫助你 - Symfony2 : How to get form validation errors after binding the request to the form

You must set error_bubbling to true in your form type by explicitly setting the option for each and every field.

+0

這並沒有幫助我 – rvaliev

+0

不,同空 「錯誤」:{}。 新增 的foreach($形式 - > getErrors()作爲$錯誤){$ 錯誤[] = $誤差; } – rvaliev

+0

顯示您的RegistrationType代碼 – Dmitry

相關問題