Skip to content

Instantly share code, notes, and snippets.

@joshavg
Created May 20, 2016 15:30
Show Gist options
  • Save joshavg/a734ac32f3d81eacb2493bd54ba85517 to your computer and use it in GitHub Desktop.
Save joshavg/a734ac32f3d81eacb2493bd54ba85517 to your computer and use it in GitHub Desktop.
Datalist in Symfony
<?php
namespace Acme\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';
}
}
{% block datalist_widget %}
<input list="{{ id }}" {{ block('widget_attributes') }}>
<datalist id="{{ id }}">
{% for choice in choices %}
<option value="{{ choice }}"></option>
{% endfor %}
</datalist>
{% endblock %}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('country', 'datalist', [choices' => ['a', 'b']]);
}
form.type.datalist_type:
class: Acme\Form\Type\DatalistType
tags:
- { name: form.type, alias: datalist }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment