我想知道是否有一种方法可以以不同于select的symfony形式“管理”多对一关联(例如,使用jQuery自动完成输入)。
我找到了,所以我阅读了有关this question
的文档这是我的变压器类
// src/AppBundle/Form/DataTransformer/ZipcodesTransformer.php
namespace AppBundle\Form\DataTransformer;
use AppBundle\Entity\Issue;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class ZipcodesTransformer implements DataTransformerInterface {
private $manager;
public function __construct(ObjectManager $manager) {
$this->manager = $manager;
}
public function transform($zipcode) {
if (null === $zipcode) {
return '';
}
return $zipcode->getId();
}
public function reverseTransform($zipcodeId) {
if (!$zipcodeId) {
return;
}
$zipcode = ->this->manager->getRepository('AppBundle:Zipcodes')->find($zipcodeId)
;
if (null === $zipcode) {
throw new TransformationFailedException(sprintf(
'An zipcode with number "%s" does not exist!',
$zipcodeId
));
}
return $zipcode;
}
}
然后选择器Type class
// src/AppBundle/Form/ZipcodeSelectorType.php
namespace AppBundle\Form;
use AppBundle\Form\DataTransformer\ZipcodesTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ZipcodeType extends AbstractType
{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new ZipcodesTransformer($this->manager);
$builder->addModelTransformer($transformer);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'invalid_message' => 'The selected ZIPCODE does not exist',
));
}
public function getParent()
{
return TextType::class;
}
}
对象形式的类是
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use AppBundle\Form\DataTransformer\ZipcodesTransformer;
class CustomerType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class ,array('label'=>'Customer Name'))
...
->add('email', EmailType::class)
->add('flag', CheckboxType::class, array('required' => false))
->add('zipcode', ZipcodeType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array('data_class' => 'AppBundle\Entity\Customer'));
}
}
感谢Kern,代码现在正确且有效。 现在为了“激活”已经测试过的jQuery自动完成,我修改了 ZipcodeSelectorType.php 并添加了这个函数
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array (
'invalid_message' => 'The selected ZIPCODE does not exist',
'attr' => array('class'=>'zipcodeac'),//ADDED THIS LINE
'placeholder'=>'Type to select a zipcode'//ADDED THIS LINE
)
);
}
但是当渲染表单时,输入具有此类
class =“zipcodeac form-control ui-autocomplete-input”
和属性
自动填充= “关闭”
并且没有解雇Ajax。
答案 0 :(得分:0)
您错过了use
课程中使用ZipcodeType
方法调用的getParent()
语句。
添加解决当前错误的use Symfony\Component\Form\Extension\Core\Type\TextType;
。
无论如何,如果你有超过数千"您评论中描述的选项,更好的方法是根据您的鉴别器字段制作自动完成的网络服务:)!