I am new to the SF2 community, so please calm down;)
I ran into a problem with JMSSerializerBundle and forms / arrays. I spent two days trying to figure it out myself, without any success, and I decided to publish it in the Group.
I am creating a simple test application that will allow me to understand how the material works. Now it's time for the API. I use FOSRestBundle, works great. My whole “application” works fine (development was very fast and efficient), I learned how to use the security component, firewalls, routing, Doctrine (however I worked with it in the past, though), writing my own authentication providers - I’m stuck in the API, in fact, part of it.
The problem with forms: I created a simple ArticleController in my APIBundle (please ignore the text answer, I just deleted my code during 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 an 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 is the problem? When sending a request in XML or JSON - when form data is not checked - I get errors (well-formed 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 a problem with the first one, I suppose. When returning a single object, I return the following XML structure:
<article>
<id>10</id>
<title>Test article</title>
</article>
when returning an array (several articles) output:
<result>
<entry>
<id>1</id>
...
</entry>
<entry>
<id>10</id>
...
</entry>
</result>
Second question: how to change the structure of the XML / JSON response?