问题描述
我正在研究一个Symfony 2.3表格,它要求一个字段只能在创建新模式下使用。
I'm working on a Symfony 2.3 form which require that a field be only available on "create new" mode.
该字段是许多一对一的关系,我设法通过使用jQuery自动完成来改变下拉列表,以便通过 FormEvents :: PRE_SET_DATA
事件使用,但是 FormEvents :: PRE_SUBMIT
将将提供的ID转换为对象需要一个Doctrine ObjectManager。
这将成为一个接口,并抛出'无法实例化接口Doctrine\Common\Persistence\ObjectManager'。
我看过以下类似的问题和文档(,,,)但是如此接近尾声(还有一个新手来到php和symfony)我希望没有重写这个作品。
The field is a link in a Many-to-One relationship, i've managed to change the dropdown list by using a jQuery AutoComplete for ease of use via a FormEvents::PRE_SET_DATA
event ,however the FormEvents::PRE_SUBMIT
which is to turn the ID supplied into an object require a Doctrine ObjectManager.This turn to be an Interface and throw the 'Cannot instantiate interface Doctrine\Common\Persistence\ObjectManager' .
I've looked at the following similar questions and documentations (Form Events , Form Events Dropdown, Doctrine Data Transformer, Dynamic Form Modification) but being so close to the end(And a newbie to both php and symfony) i wish no to re-write this piece.
可以在下面的事件监听器和数据变换器类中找到: p>
May you find below my Event Listener and Data Transformer classes:
namespace XXXXXXXBundle\Form\EventListener;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use XXXXXXXXXXXXXBundle\Form\DataTransformer\rneToEcoleTransformer;
class AddFieldSubscriber implements EventSubscriberInterface {
private $manager;
public static function getSubscribedEvents() {
// Tells the dispatcher that you want to listen on the form.pre_set_data
// event and that the preSetData method should be called.
return array(\Symfony\Component\Form\FormEvents::PRE_SET_DATA => 'preSetData',
\Symfony\Component\Form\FormEvents::PRE_SUBMIT => 'preSubmit');
}
public function preSetData(\Symfony\Component\Form\FormEvent $event) {
$parents = $event->getData();
$form = $event->getForm();
//Verifie si le parent d'eleve est un nouveau
//ou s'il s'agit d' une mise à jour
if (!$parents || null === $parents->getId()) {
$form->add('ecole', 'text', array('invalid_message' => 'Ce RNE n\'est pas valide',
'attr' => array('data-id' => 'ecoles',
'data-url' => 'parents_json_ecoles'
)
)
);
}
}
public function preSubmit(\Symfony\Component\Form\FormEvent $event) {
$this->manager = new \Doctrine\Common\Persistence\ObjectManager;
$parents = $event->getData();
if (empty($parents['ecole'])) {
return;
}
$event->getForm()->get('ecole')->getConfig()->addModelTransformer(new rneToEcoleTransformer($this->manager));
}}
namespace XXXXXXXBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class rneToEcoleTransformer implements DataTransformerInterface {
private $manager;
public function __construct(\Doctrine\Common\Persistence\ObjectManager $manager) {
$this->manager = $manager;
}
public function transform($ecole) {
if (null === $ecole) {
return '';
}
return $ecole->getRne();
}
public function reverseTransform($rneNumber) {
if (!$rneNumber) {
return;
}
$ecole = $this->manager
->getRepository('XXXXXXXBundle:Ecole')
->find($rneNumber);
if (null === $ecole) {
throw new TransformationFailedException(sprintf(
'No matching data was found "%s" !', $rneNumber
));
}
return $ecole;
}}
以下是使用的操作:
private function createCreateForm(Parents $entity) {
$manager = $this->getDoctrine()->getManager();
$form = $this->createForm(new ParentsType($manager), $entity, array(
'action' => $this->generateUrl('parents_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Add'));
return $form;
}
Formtype:
class XXXXType extends AbstractType {
private $manager;
public function __construct(\Doctrine\Common\Persistence\ObjectManager $manager) {
$this->manager = $manager;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nom', 'text', array('label' => 'Nom *',
'attr' => array('placeholder' => 'Nom ',
'maxlength' => 250,
'trim' => true)
)
)
->add('prenoms', 'text', array('label' => 'Prénom(s) *',
'attr' => array('placeholder' => 'Prénom(s)',
'maxlength' => 250,
'trim' => true)
)
)
->add('date_de_naiss', 'date', array('widget' => 'single_text', 'format' => 'dd-MM-yyyy',
'label' => 'Date de naissance ',
'required' => false, 'data' => null,
'attr' => array('name' => 'datepicker')
)
)
->add('telephone', 'text', array('label' => 'Telephone *',
'attr' => array('placeholder' => '123-456-7890 or 1234567890')
)
)
->add('email', 'email', array('attr' => array('placeholder' => '[email protected]',),
'trim' => true,
'required' => false,
'data' => null
)
);
$builder->addEventSubscriber(new AddFieldSubscriber($manager));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'XXXXXXBundle\Entity\Parents'
));
}
/**
* @return string
*/
public function getName() {
return 'XXXXXXXXXsbundle_parents';
}
}
推荐答案
我终于设法通过使用数据变压器而不是事件订阅者进行工作,如下所示:
`
I finally managed to make it work by using the data transformer instead of the event subscriber as follow:
`
class ParentsType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nom', 'text', array('label' => 'Nom *',
'attr' => array('placeholder' => 'Nom du Parent d\'élève',
'maxlength' => 250,
'trim' => true)
)
)
->add('prenoms', 'text', array('label' => 'Prénom(s) *',
'attr' => array('placeholder' => 'Prénom(s) du parent d\' élève',
'maxlength' => 250,
'trim' => true)
)
)
->add('date_de_naiss', 'date', array('widget' => 'single_text', 'format' => 'dd-MM-yyyy',
'label' => 'Date de naissance ',
'required' => false, 'data' => null,
'attr' => array('name' => 'datepicker')
)
)
->add('telephone', 'text', array('label' => 'Telephone *',
'attr' => array('placeholder' => '123-456-7890 or 1234567890')
)
)
->add('email', 'email', array('attr' => array('placeholder' => '[email protected]',),
'trim' => true,
'required' => false,
'data' => null
)
);
$entityManager = $options['em'];
$transformer = new idToEcoleTransformer($entityManager);
$builder->add(
$builder->create('ecole','text',array('required' => true,
'attr' => array( 'data-id'=>'ecoles',
'data-url'=>'parents_json_ecoles',)
)
)->addModelTransformer($transformer)
);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'XXXXXXXXBundle\Entity\Parents',
'csrf_protection' => false,
'validation_groups' => array('filtering')
));
$resolver->setRequired(array('em'));
$resolver->setAllowedTypes(array('em'=>'Doctrine\Common\Persistence\ObjectManager',));
}
/**
* @return string
*/
public function getName() {
return 'parentsbundle_parents';
}
`
这样我就不需要通过构造函数传递 ObjectManager ,只是使用 Resolver
With this i don't need to pass the ObjectManager via the constructor , but just to use the Resolver
这篇关于Symfony2表单事件侦听器和数据变换器错误无法实例化接口Doctrine\ .... \ObjectManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!