|
@@ -18,6 +18,8 @@
|
|
|
|
|
|
namespace JMS\SerializerBundle\Serializer\Handler;
|
|
|
|
|
|
+use JMS\SerializerBundle\Serializer\GenericSerializationVisitor;
|
|
|
+
|
|
|
use Symfony\Component\Form\Form;
|
|
|
use Symfony\Component\Form\FormError;
|
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
|
@@ -37,51 +39,64 @@ class FormErrorHandler implements SerializationHandlerInterface
|
|
|
public function serialize(VisitorInterface $visitor, $data, $type, &$handled)
|
|
|
{
|
|
|
if ($data instanceof Form) {
|
|
|
- $handled = true;
|
|
|
-
|
|
|
if ($visitor instanceof XmlSerializationVisitor) {
|
|
|
+ $handled = true;
|
|
|
+
|
|
|
if (null === $visitor->document) {
|
|
|
$visitor->document = $visitor->createDocument(null, null, false);
|
|
|
+ $visitor->document->appendChild($formNode = $visitor->document->createElement('form'));
|
|
|
+ $visitor->setCurrentNode($formNode);
|
|
|
+ } else {
|
|
|
+ $visitor->getCurrentNode()->appendChild(
|
|
|
+ $formNode = $visitor->document->createElement('form')
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
- $formNode = $visitor->document->createElement('form');
|
|
|
$formNode->setAttribute('name', $data->getName());
|
|
|
|
|
|
- if (null === $visitor->getCurrentNode()) {
|
|
|
- $visitor->document->appendChild($formNode);
|
|
|
+ $formNode->appendChild($errorsNode = $visitor->document->createElement('errors'));
|
|
|
+ foreach ($data->getErrors() as $error) {
|
|
|
+ $errorNode = $visitor->document->createElement('entry');
|
|
|
+ $errorNode->appendChild($this->serialize($visitor, $error, null, $visited));
|
|
|
+ $errorsNode->appendChild($errorNode);
|
|
|
}
|
|
|
- else {
|
|
|
- $visitor->getCurrentNode()->appendChild($formNode);
|
|
|
+
|
|
|
+ foreach ($data->getChildren() as $child) {
|
|
|
+ if (null !== $node = $this->serialize($visitor, $child, null, $visited)) {
|
|
|
+ $formNode->appendChild($node);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // append errors node
|
|
|
- $formNode->appendChild($errorsNode = $visitor->document->createElement('errors'));
|
|
|
- $visitor->setCurrentNode($errorsNode);
|
|
|
- $visitor->visitArray($data->getErrors(), $type);
|
|
|
- $visitor->revertCurrentNode();
|
|
|
+ return;
|
|
|
+ } else if ($visitor instanceof GenericSerializationVisitor) {
|
|
|
+ $handled = true;
|
|
|
+ $isRoot = null === $visitor->getRoot();
|
|
|
|
|
|
- if ($data->hasChildren()) {
|
|
|
- $visitor->setCurrentNode($formNode);
|
|
|
+ $form = $errors = array();
|
|
|
+ foreach ($data->getErrors() as $error) {
|
|
|
+ $errors[] = $this->serialize($visitor, $error, null, $visited);
|
|
|
+ }
|
|
|
|
|
|
- foreach ($data->getChildren() as $child) {
|
|
|
- $this->serialize($visitor, $child, $type, $visited);
|
|
|
- }
|
|
|
+ if ($errors) {
|
|
|
+ $form['errors'] = $errors;
|
|
|
+ }
|
|
|
|
|
|
- $visitor->revertCurrentNode();
|
|
|
+ $children = array();
|
|
|
+ foreach ($data->getChildren() as $child) {
|
|
|
+ $children[$child->getName()] = $this->serialize($visitor, $child, null, $visited);
|
|
|
}
|
|
|
|
|
|
- return $formNode;
|
|
|
- }
|
|
|
- else {
|
|
|
- $form = array('errors' => $data->getErrors());
|
|
|
- if ($data->hasChildren()) {
|
|
|
- $form['children'] = $data->getChildren();
|
|
|
+ if ($children) {
|
|
|
+ $form['children'] = $children;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($isRoot) {
|
|
|
+ $visitor->setRoot($form);
|
|
|
}
|
|
|
|
|
|
- return $visitor->visitArray($form, $type);
|
|
|
+ return $form;
|
|
|
}
|
|
|
- }
|
|
|
- else if ($data instanceof FormError) {
|
|
|
+ } else if ($data instanceof FormError) {
|
|
|
$handled = true;
|
|
|
$message = $this->translator->trans($data->getMessageTemplate(), $data->getMessageParameters(), 'validators');
|
|
|
|