How can I handle adding a from element from an entity with a property of my choice as a value and another one as a displayed value?
->add('locale', 'entity', array(
'class' => 'PrUserBundle:Language',
'property' => 'name','required' => true,
'data' => $userData->getLocale()
))
will result in
<option value="3">English</option>
I would like to have
<option value="en">English</option>
Can I handle this by the property or inside the entitiy which looks like
/**
* @ORM\Entity
* @ORM\Table(name="language")
*/
class Language {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/** @ORM\column(type="string", length=200) */
protected $locale;
/** @ORM\column(type="string", length=200) */
protected $name;
I think data transformer is the best way to do that.
http://symfony.com/doc/current/cookbook/form/data_transformers.html
First, you create the DataTransformer that will handle the transformation from "Language" entity to his "locale" string (which have to be unique).
In your case, the transformer should look like this :
<?php
namespace YouBundle\Form\DataTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class StringToLanguageTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
/**
* @param string $locale
* @return Language|null
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function transform($locale)
{
// Here, you can use Object manager, get your repository et get the "Language" Entity with your local string
// return the entity or thow a TransformationFailedException if none are found
}
/**
* @param Language $language
* @return string
*/
public function reverseTransform($language)
{
if (null === $language) {
return "";
}
return $language->getLocale();
}
}
To use this, create a custom form Type that will extends the "entity" form type.
namespace YouBundle\Form;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use YouBundle\Form\DataTransformer\StringToLanguageTransformer;
class LanguageType extends AbstractType
{
/** @var \Doctrine\ORM\EntityManager */
protected $entityManager;
/**
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$languageTransformer = new StringToLanguageTransformer($this->entityManager);
$builder->addModelTransformer($languageTransformer);
}
/**
* @return string
*/
public function getParent()
{
return 'entity';
}
/**
* @return string
*/
public function getName()
{
return 'language_entity';
}
}
Declare this new form type as a service :
your_bundle.form.type.language_entity:
class: YourBundle\Form\LanguageType
tags:
- { name: form.type, alias: language_entity }
arguments: [@doctrine.orm.entity_manager]
Now, you can use it like classic entity type (but this one will use locale instead of id where generating your drop down.
->add('locale', 'language_entity ', array(
'class' => 'PrUserBundle:Language',
'property' => 'name','required' => true
))
I hope this will help you.
Best regards
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments