当前位置: 动力学知识库 > 问答 > 编程问答 >

symfony2 - Could not load type "locality"

问题描述:

I hope to use a form inside my controller,but I get everytime the following error :

Could not load type "locality"

and here is ly form class :

class LocationType extends AbstractType

{

public function buildForm(FormBuilderInterface $builder, array $options)

{

$factory = $builder->getFormFactory();

$builder->add('province','entity',array(

'class' => 'Acme\DemoBundle\Entity\Province',

'property' => 'name'));

$refreshLocality = function ($form, $province) use ($factory) {

$form->add($factory->createNamed('entity','locality',null, array(

'class' => 'Acme\DemoBundle\Entity\Locality',

'property' => 'name',

'label' => 'Locality',

'query_builder' => function (EntityRepository $repository) use ($province) {

$qb = $repository->createQueryBuilder('locality')

->innerJoin('locality.province', 'province');

if($province instanceof Province) {

$qb = $qb->where('locality.province = :province')

->setParameter('province', $province);

} elseif(is_numeric($province)) {

$qb = $qb->where('province.id = :province_id')

->setParameter('province_id', $province);

} else {

$qb = $qb->where('province.id = 1');

}

return $qb;

}

)));

};

$builder->add('address','text',array(

'required' => false));

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($refreshLocality) {

$form = $event->getForm();

$data = $event->getData();

if($data == null)

$refreshLocality($form, null); //As of beta2, when a form is created setData(null) is called first

if($data instanceof Location) {

$refreshLocality($form, $data->getLocality()->getProvince());

}

});

$builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) use ($refreshLocality) {

$form = $event->getForm();

$data = $event->getData();

if(array_key_exists('province', $data)) {

$refreshLocality($form, $data['province']);

}

});

}

public function setDefaultOptions(OptionsResolverInterface $resolver)

{

$resolver->setDefaults(array(

'data_class' => 'Acme\DemoBundle\Entity\Location'

));

}

public function getName()

{

return 'acme_demobundle_locationtype';

}

}

Then I called this class in my controller :

 public function indexAction()

{

$form = $this->get('form.factory')->create(new \Acme\DemoBundle\Form\LocationType());

$request = $this->get('request');

if ($request->isMethod('POST')) {

$form->bind($request);

if ($form->isValid()) {

}

}

return array('form' => $form->createView());;

}

and here my twig :

 <form action="{{ path('_demo') }}" method="POST" id="contact_form">

{{ form_errors(form) }}

{{ form_rest(form) }}

<input type="submit" value="Send" class="symfony-button-grey" />

</form>

when I had the error above I tried to register your form in the section services in service.xml :

 <service id="form.type.acme_demobundle_locationtype" class="Acme\DemoBundle\Form\LocationType">

<tag name="form.type" alias="acme_demobundle_locationtype" />

</service>

but I get the same error,any idea?

网友答案:

You need to swap the arguments that you passed to createNamed():

$form->add($factory->createNamed('entity','locality',null, array(

should be

$form->add($factory->createNamed('locality', 'entity', null, array(

In fact, you can even simplify your code to

$form->add('locality', 'entity', array(
分享给朋友:
您可能感兴趣的文章:
随机阅读: