Hola, tengo la tipica relacion Pais > Estado > Ciudad, los tres elementos son SELECT. Las opciones de estos elementos se construyen de forma dinámica por lo cual he optado por usar `DataTransformers` y tratar de evitar el mensaje "This value is not valid" cuando envio el formulario. Este es el codigo detras de `
AbstractType,
Symfony\Component\Form\FormBuilderInterface,
Symfony\Component\OptionsResolver\OptionsResolverInterface,
Common\CommonBundle\Form\AddressExtraInfoType;
class StandardAddressType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('country', 'country_choice')
->add('state', 'state_choice')
->add('city', 'city_choice')
->add('extra_info', new AddressExtraInfoType());
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Common\CommonBundle\Entity\StandardAddress'
));
}
public function getName() {
return 'standard_address';
}
}
Y este el codigo detras de `
CountryToNumberDataTransformer` y`
ChoiceType`
`CountryToNumberTransformer.php` namespace Common\CommonBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface,
Symfony\Component\Form\Exception\TransformationFailedException,
Doctrine\Common\Persistence\ObjectManager,
Common\CommonBundle\Entity\Country;
class CountryToNumberTransformer implements DataTransformerInterface {
private $om;
public function __construct(ObjectManager $om) {
$this->om = $om;
}
public function transform($country) {
if (null === $country) {
return "";
}
return $country->getIso();
}
public function reverseTransform($number) {
if (!$number) {
return null;
}
$country = $this->om
->getRepository('CommonBundle:Country')
->findOneBy(array('iso' => $number));
if (null === $country) {
throw new TransformationFailedException(sprintf(
'The country with ISO "%s" does not exist!', $number
));
}
return $country;
}
}
`CountryChoiceType.php` namespace Common\CommonBundle\Form\Type;
use Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormBuilderInterface,
Doctrine\Common\Persistence\ObjectManager,
Symfony\Component\OptionsResolver\OptionsResolverInterface,
Common\CommonBundle\Form\DataTransformer\CountryToNumberTransformer;
class CountryChoiceType extends AbstractType {
private $om;
public function __construct(ObjectManager $om) {
$this->om = $om;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$transformer = new CountryToNumberTransformer($this->om);
$builder->addModelTransformer($transformer);
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
"class" => "CommonBundle:Country",
"property" => "name",
"label" => "País",
"required" => false,
"empty_value" => "Seleccione un país",
"attr" => array(
"ng-options" => "
country.name for country in countries.entities",
"tooltip-trigger" => "focus",
"tooltip-placement" => "right",
"wv-cur" => "",
"wv-err" => "Error!",
"wv-req" => "The value you selected is not a
valid choice", //El valor seleccionado no es una opción válida
"type" => "text",
"class" => "country"
)
));
}
public function getParent() {
return 'choice';
}
public function getName() {
return 'country_choice';
}
}
Y por supuesto el tipo esta registrado como un servicio:
common_bundle.type.country_choice:
class: Wuelto\Common\CommonBundle\Form\Type\CountryChoiceType
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: form.type, alias: country_choice }
Cada vez que envio el formulario obtengo el error:
>> "This value is not valid."
Que asumo esta asociado a la validacion, como puedo solucionar esto? He leido la documentacion y he hecho las cosas como ellos sugieren pero me sigue sin funcionar y el formulario por ende nunca valida. Sugerencias? Soluciones?