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 Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
/**
* CustomFieldsGroup controller.
@ -210,17 +211,25 @@ class CustomFieldsGroupController extends Controller
$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',
'action' => $this->generateUrl('customfield_new',
array('cfGroup' => $customFieldsGroup->getId())),
'action' => $this->generateUrl('customfield_new'),
'csrf_protection' => false
))
->add('type', 'choice', array(
'choices' => $fieldChoices
))
->add('submit', 'submit')
->getForm();
->add('customFieldsGroup', 'hidden')
->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) {
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();
}
@ -49,6 +57,14 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
if (!$id) {
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
->getRepository('ChillCustomFieldsBundle:customFieldsGroup')->find($id)