【问题标题】:REST form validation in symfony 2 - how to test postsymfony 2 中的 REST 表单验证 - 如何测试帖子
【发布时间】:2014-03-07 02:20:22
【问题描述】:

表单永远无效,但$form->getErrors() 没有给出错误。 因为它将是一个 REST API 使用 DEV HTTP 客户端对其进行测试

测试数据是

标题: Content-Type:application/json

正文: {"username":"sad","password":"123","email":"asdsads@fgfdg.com"}

我没有任何 validation.yml 文件

有什么方法可以找出问题所在(错误消息)吗?

public function postUserAction(Request $request)
{

    return $this->processForm(new User(),$request);
}

private function processForm(User $user, Request $request )
{


    $form = $this->createForm(new UserType(), $user);
    $form->handleRequest($request);

    if ($form->isValid()) {

         return array('form' => 'valid');
    }

    return \FOS\RestBundle\View\View::create($form->getErrors(),400);
}

【问题讨论】:

    标签: validation rest symfony


    【解决方案1】:

    经过一点调试: isValid() 检查表单的几件事,例如表单是否已提交。不是,所以我改成

    ...
            $form = $this->createForm(new UserType(), $user);
            //$form->handleRequest($request);
            $form->submit($request);
    
            if ($form->isValid()) {
    ...
    

    现在它是有效的。

    【讨论】:

    • 如果您在可选的第三个参数中添加一些选项以使用该方法创建表单,例如 array("method" => "POST"),您可能仍然可以使用 handleRequest()。这是我能够使用 handleRequest() 提交表单的唯一方法。我加倍努力让 handleRequest() 工作,因为从 symfony 2.3 开始不推荐使用 $request 作为参数调用 submit() 并且显然将在 3.0 中消失。
    【解决方案2】:

    在调试表单验证时,

    使用$form->getErrorsAsString() 而不是$form->getErrors()

    (这将深入到包括子窗体在内的深层。)

    【讨论】:

    • 谢谢。看起来没有错误:“form”:“用户名:\n没有错误\n密码:\n没有错误\nemail:\n没有错误\n”,但是表单仍然无效...
    【解决方案3】:

    这对我有用:

    $form = $this->createForm(new UserType(), $user);
    $jsonData = json_decode($request->getContent(), true); // "true" to get an associative array
    
    if ('POST' === $request->getMethod()) {
        $form->bind($jsonData);
        ...
    }
    

    【讨论】:

      【解决方案4】:

      我迟到了回复这个但是,所有的答案都是错误的。应避免使用submit,而应使用handleRequestRead this。该表格对handleRequest 无效,因为UserType 缺少下一个:

      1. 缺少表单域
      2. 表单域的格式不合适
      3. UserType 类中,getName 函数应该返回空字符串并且应该禁用csrf_protection(如果你使用API 方法,其中form 是来自 Symfony2 项目的not generated):

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'UserApiBundle\Entity\User',
                'csrf_protection'   => false,
            ));
        }
        
        public function getName()
        {
            return '';
        }
        

        submit 不会对此进行检查,这对于POST 来说是一种不好的方法。您可以将它用于PUT 请求以更新User,其中password 字段由于xyz 原因(例如:权限)不会提交。

        $form->submit($request, false);
        

      很难在 Symfony2 中挖掘 form 错误。您可以使用此函数来获取错误:

      public function getErrors($form)
      {
          $errors = array();
          foreach ($form as $fieldName => $formField) {
              $currentError = $formField->getErrors();
      
              if ($currentError->current()) {
                  $current = $currentError->current();
                  $errors[$fieldName] = $current->getMessage();
              }
          }
      
          return $errors;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2014-11-30
        • 2014-04-30
        相关资源
        最近更新 更多