JMSSerializer +表​​单/数组 [英] JMSSerializer + forms/arrays

查看:78
本文介绍了JMSSerializer +表​​单/数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SF2社区的新手,所以请放轻松;)

I'm rather newcomer into SF2 community, so please, take it easy ;)

我遇到了JMSSerializerBundle和表单/数组的问题.我花了两天的时间自己弄清楚,但没有成功,因此决定将其发布到小组.

I've faced an issue with JMSSerializerBundle and forms/arrays. I've spent two days trying to figure it out myself, without any success and I've decided to post this to the Group.

我正在构建一个简单的测试应用程序,它将使我能够理解这些东西是如何工作的.现在是时候使用API​​了.我正在使用FOSRestBundle,效果很好.我的整个应用程序"都运行良好(开发非常快速且有效),我学会了如何使用安全组件,防火墙,路由,Doctrine(不过我过去曾使用过),编写了自定义身份验证提供程序-我实际上卡在了API上.

I'm building a simple test application that will allow me to understand how the stuff works. Now it's time for API. I'm using FOSRestBundle, works perfectly. My whole "application" is working perfectly (development was really fast and effective), I've learned how to use Security component, firewalls, routing, Doctrine (however I worked with it in the past though), writing custom authentication providers - I'm stuck at API, in fact, a part of it.

表格问题: 我在APIBundle中创建了简单的ArticleController(请忽略文本响应,我在调试时刚刚删除了代码,以使其更具可读性):

Forms issue: I've created simple ArticleController in my APIBundle (please ignore text response, I've just removed my code in while debugging to make it more readable):

namespace Veron\ApiBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use Veron\ApiBundle\Form\Type\ArticleType;
use Veron\CoreBundle\Entity\Article;
class ArticleController extends Controller
{
    public function createAction()
    {
        return $this->processForm(new Article());
    }
    private function processForm(Article $article)
    {
        $em = $this->getDoctrine()->getManager();
        $form = $this->createForm(new ArticleType(), $article, array(
            'csrf_protection' => false
        ));
        $form->bind($this->getRequest());
        if ($form->isValid()) {
            return new Response('Everything ok');
        }
        $view = View::create($form, 400);
        return $this->get('fos_rest.view_handler')->handle($view);
    }
}

如您所见,我还有一个ArticleType表单类:

As you can see, I also have a ArticleType form class:

namespace Veron\ApiBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('description')
        ;
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'        => 'Veron\CoreBundle\Entity\Article',
            'csrf_protection'   => false,
        ));
    }
    public function getName()
    {
        return 'article';
    }
}

问题是什么?在以XML或JSON发送请求时-当表单数据未通过验证时-我遇到了错误(由JMSSerializer很好地格式化):

what the issue is? While sending a request, in XML or JSON - when the form data is not being validated - I'm getting the errors (well formated by JMSSerializer):

JSON示例:

{"errors":["This value should not be blank."],"children":{"title":{"errors":["This value is too short. It should have 5 character or more."]},"description":{"errors":["This value should not be blank."]}}}

XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<form name="article">
  <errors>
    <entry><![CDATA[This value should not be blank.]]></entry>
  </errors>
  <form name="title">
    <errors>
      <entry><![CDATA[This value should not be blank.]]></entry>
    </errors>
  </form>
  <form name="description">
    <errors>
      <entry><![CDATA[This value should not be blank.]]></entry>
    </errors>
  </form>
</form>

我的第一个问题是:是否有任何自动方法来更改序列化表格错误的输出?

My first question is: is there any automated way to change the output of serialized form errors?

我想我也有一个与第一个有关的问题.返回单个对象时,我返回了以下XML结构:

I also have an issue, related to the first one, I suppose. When returning single object, I have following XML structure returned:

<article>
    <id>10</id>
    <title>Test article</title>
</article>

返回一个数组(多篇文章)时,输出为:

while returning an array (multiple articles) the output is:

<result>
    <entry>
        <id>1</id>
        ...
    </entry>
    <entry>
        <id>10</id>
        ...
    </entry>
</result>

第二个问题:如何更改响应XML/JSON结构?

Second question: how to change response XML/JSON structure?

推荐答案

使用JMSSerializer呈现表单错误的方法是通过此类:

The rendering of form errors with the JMSSerializer is handled bu this class : https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/Handler/FormErrorHandler.php . You could probably write you own.

关于结构,是的,您可以更改它,但是您究竟想做什么?您可以查看文档以了解更多信息: http://jmsyst.com/libs/serializer/master/reference/annotations (请注意,您也可以使用xml/yml配置,但是注释上的文档更加完整)

And about the structure, yes you can change it, but what exactly do you want to do ? You can have a look at the documentation to learn more about it : http://jmsyst.com/libs/serializer/master/reference/annotations (Note that you can also use xml/yml configuration, but the doc is more complete on the annotation)

这篇关于JMSSerializer +表​​单/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆