对于我的工程学校的项目,我必须创建具有自动完成功能的搜索引擎,该功能可以从网页搜索Spotify数据库中的歌曲。 为此,我创建了一个新的字段类型“datalist”,当用户在字段中按字母时显示选项列表。
我已按照多个网站的说明创建了一个datalistType
课程,我将其放在此处,并在service.yml
文件中添加了该服务。
尽管如此,我已按照所有说明进行操作,但我始终存在此错误:
“无法加载类型”datalist“。
以下是代码:
// config.yml
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form_themes:
- 'AppBundle:Form:fields.html.twig'
form:
resources: - 'AppBundle:Form:fields.html.twig'
// AppBundle\Form\Type\datalistType.php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class datalistType extends AbstractType {
public function getParent()
{
return 'text';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired(['choices']);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['choices'] = $options['choices'];
}
public function getName()
{
return 'datalist';
}
}
// AppBundle\Controller\FormController.php
namespace AppBundle\Controller;
use AppBundle\Form\Type\formType;
use AppBundle\Entity\countries;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FormController extends Controller
{
public function formAction(Request $request)
{
$form = $this->createForm(new formType());
if($request->getMethod() == 'POST')
{
$form->bind($this->get('request'));
if ($form->isValid())
{
$em = $this->get('doctrine')->getManager();
$country = new countries();
$country->setName($form->getData()['title']);
$em->persist($country);
$em->flush();
}
}
return $this->render('AppBundle:Form:form.html.twig',array('form' => $form->createView()));
}
}
// AppBundle\FormType\formType.php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Form\Type\datalistType;
class formType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$merde = new datalistType();
$builder->add('title');
$builder->add('ajouter', 'submit');
$builder->add('alphabet', 'datalist');
}
}