I'm building an API using FOSRestBundle for adding products to a basket. For the sake of keeping this example simple we have a range of products which come in different sizes.
I'd like to be able to specify the size code in the JSON request. For example:
{
"product": 3,
"size": "S"
}
(I'd also like to use the product code instead of the database ID, but that's for another day!)
Other parts of the project I have done similar tasks using data transformers, but these were simpler forms where the values didn't change based on other fields selected values.
So my current basket form...
class BasketAddType extends AbstractType
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('product', 'entity', [
'class' => 'CatalogueBundle:Product',
]);
$builder->get('product')->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit']);
}
public function onPostSubmit(FormEvent $event)
{
// this will be Product entity
$form = $event->getForm();
$this->addElements($form->getParent(), $form->getData());
}
protected function addElements(FormInterface $form, Product $product = null)
{
if (is_null($product)) {
$sizes = [];
} else {
$sizes = $product->getSizes();
}
$form
->add('size', 'size', [
'choices' => $sizes
]);
}
public function getName()
{
return '';
}
}
The custom size form type I'm using above is so I can add the model transformer. As found in this answer https://stackoverflow.com/a/19590707/3861815
class SizeType extends AbstractType
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new SizeTransformer($this->em));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'class' => 'CatalogueBundle\Entity\Size'
]);
}
public function getParent()
{
return 'entity';
}
public function getName()
{
return 'size';
}
}
And finally the transformer.
class SizeTransformer implements DataTransformerInterface
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function transform($size)
{
if (null === $size) {
return '';
}
return $size->getCode();
}
public function reverseTransform($code)
{
// WE NEVER GET HERE?
$size = $this->em->getRepository('CatalogueBundle:Size')
->findOneByCode($code);
if (null === $size) {
throw new TransformationFailedException('No such size exists');
}
return $size;
}
}
So I did a quick exit;
in reverseTransform
and it's never fired so I will always get an error on the size element about it being invalid.
What would be the best way to getting a data transformer onto the size field here?
So the problem was that I was using an entity type instead of a text type when using the model data transformer.
Here is my working code albeit probably not perfect, the primary form
class BasketAddType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('product', 'entity', [
'class' => 'CatalogueBundle:Product'
]);
$builder->get('product')->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit']);
}
public function onPostSubmit(FormEvent $event)
{
$form = $event->getForm()->getParent();
$product = $event->getForm()->getData();
$form
->add('size', 'size', [
'sizes' => $product->getSizes()->toArray() // getSizes() is an ArrayCollection
);
}
public function getName()
{
return '';
}
}
My custom form size type which applies the model transformer with the provided size options.
class SizeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new SizeTransformer($options['sizes']));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired([
'sizes'
]);
}
public function getParent()
{
return 'text';
}
public function getName()
{
return 'size';
}
}
And finally the size transformer.
class SizeTransformer implements DataTransformerInterface
{
protected $sizes;
public function __construct(array $sizes)
{
$this->sizes = $sizes;
}
public function transform($size)
{
if (null === $size) {
return '';
}
return $size->getCode();
}
public function reverseTransform($code)
{
foreach ($this->sizes as $size) {
if ($size->getCode() == $code) {
return $size;
}
}
throw new TransformationFailedException('No such size exists');
}
}
This solution wouldn't work too well if there were a high number of sizes available for each product. Guess if that was the case I'd need to pass both the EntityManager and the product into the transformer and query the DB accordingly.
This is a dependent fields
I have a Product entity wich in relation with a Category entity which in relation with a Collection entity
here is my code for the add product form
class ProductsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$propertyPathToCategory = 'category';
$builder
->add('title')
->add('description','textarea')
->add('collection','entity', array(
'class' => 'FMFmBundle:Collections',
'empty_value' => 'Collection',
'choice_label' => 'title'
))
->addEventSubscriber(new AddCategoryFieldSubscriber($propertyPathToCategory));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'FM\FmBundle\Entity\Products'
));
}
public function getName()
{
return 'fm_fmbundle_products';
}
}
the AddCategoryFieldSubscriber
//Form/EventListener
class AddCategoryFieldSubscriber implements EventSubscriberInterface
{
private $propertyPathToCategory;
public function __construct($propertyPathToCategory)
{
$this->propertyPathToCategory = $propertyPathToCategory;
}
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_SUBMIT => 'preSubmit'
);
}
private function addCategoryForm($form, $collection_id)
{
$formOptions = array(
'class' => 'FMFmBundle:Categories',
'empty_value' => 'Category',
'label' => 'Category',
'attr' => array(
'class' => 'Category_selector',
),
'query_builder' => function (EntityRepository $repository) use ($collection_id) {
$qb = $repository->createQueryBuilder('category')
->innerJoin('category.collection', 'collection')
->where('collection.id = :collection')
->setParameter('collection', $collection_id)
;
return $qb;
}
);
$form->add($this->propertyPathToCategory, 'entity', $formOptions);
}
public function preSetData(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$accessor = PropertyAccess::createPropertyAccessor();
$category = $accessor->getValue($data, $this->propertyPathToCategory);
$collection_id = ($category) ? $category->getCollection()->getId() : null;
$this->addCategoryForm($form, $collection_id);
}
public function preSubmit(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
$collection_id = array_key_exists('collection', $data) ? $data['collection'] : null;
$this->addCategoryForm($form, $collection_id);
}
}
add new action in the controller
public function SelectCategoryAction(Request $request)
{
$collection_id = $request->get('collecton_id');
$em = $this->getDoctrine()->getManager();
$categories = $em->getRepository('FMFmBundle:Categories')->findByCollection($collection_id);
$Jcategories=array();
foreach($categories as $category){
$Jcategories[]=array(
'id' => $category->getId(),
'title' => $category->getTitle()
);
}
return new JsonResponse($Jcategories);
}
add new route for the action
select_category:
path: /selectcategory
defaults: { _controller: FMFmBundle:Product:SelectCategory }
and some ajax
$("#collectionSelect").change(function(){
var data = {
collecton_id: $(this).val()
};
$.ajax({
type: 'post',
url: 'selectcategory',
data: data,
success: function(data) {
var $category_selector = $('#categorySelect');
$category_selector.empty();
for (var i=0, total = data.length; i < total; i++)
$category_selector.append('<option value="' + data[i].id + '">' + data[i].title + '</option>');
}
});
});
References: