fix form to jump to create a custom field

This commit is contained in:
Julien Fastré 2015-11-07 09:38:12 +01:00
parent 59ce457131
commit e1435d0883
2 changed files with 30 additions and 5 deletions

View File

@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup; use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Chill\CustomFieldsBundle\Entity\CustomField; use Chill\CustomFieldsBundle\Entity\CustomField;
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
/** /**
* CustomFieldsGroup controller. * CustomFieldsGroup controller.
@ -210,17 +211,25 @@ class CustomFieldsGroupController extends Controller
$fieldChoices[$key] = $customType->getName(); $fieldChoices[$key] = $customType->getName();
} }
return $this->createFormBuilder(new CustomField(), array( $customfield = (new CustomField())
->setCustomFieldsGroup($customFieldsGroup);
$builder = $this->get('form.factory')
->createNamedBuilder(null, 'form', $customfield, array(
'method' => 'GET', 'method' => 'GET',
'action' => $this->generateUrl('customfield_new', 'action' => $this->generateUrl('customfield_new'),
array('cfGroup' => $customFieldsGroup->getId())),
'csrf_protection' => false 'csrf_protection' => false
)) ))
->add('type', 'choice', array( ->add('type', 'choice', array(
'choices' => $fieldChoices 'choices' => $fieldChoices
)) ))
->add('submit', 'submit') ->add('customFieldsGroup', 'hidden')
->getForm(); ->add('submit', 'submit');
$builder->get('customFieldsGroup')
->addViewTransformer(new CustomFieldsGroupToIdTransformer(
$this->getDoctrine()->getManager()));
return $builder->getForm();
} }
/** /**

View File

@ -33,6 +33,14 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
if (null === $customFieldsGroup) { if (null === $customFieldsGroup) {
return ""; return "";
} }
if (!$customFieldsGroup instanceof CustomFieldsGroup) {
throw new TransformationFailedException(sprintf('Transformation failed: '
. 'the expected type of the transforme function is an '
. 'object of type Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
. '%s given (value : %s)', gettype($customFieldsGroup),
$customFieldsGroup));
}
return $customFieldsGroup->getId(); return $customFieldsGroup->getId();
} }
@ -49,6 +57,14 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
if (!$id) { if (!$id) {
return null; return null;
} }
if ($id instanceof CustomFieldsGroup) {
throw new TransformationFailedException(sprintf(
'The transformation failed: the expected argument on '
. 'reverseTransform is an object of type int,'
. 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
. 'given', gettype($id)));
}
$customFieldsGroup = $this->om $customFieldsGroup = $this->om
->getRepository('ChillCustomFieldsBundle:customFieldsGroup')->find($id) ->getRepository('ChillCustomFieldsBundle:customFieldsGroup')->find($id)