Domanda

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

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.

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.

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);
    }
}

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';
    }
}

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 example:

{"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 example:

<?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?

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>

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

È stato utile?

Soluzione

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.

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)

Altri suggerimenti

You just need to change your Annotations to xmllist:

@Serializer\XmlList(inline = true, entry = "article")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top