Created
May 20, 2016 15:30
-
-
Save joshavg/a734ac32f3d81eacb2493bd54ba85517 to your computer and use it in GitHub Desktop.
Datalist in Symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% block datalist_widget %} | |
<input list="{{ id }}" {{ block('widget_attributes') }}> | |
<datalist id="{{ id }}"> | |
{% for choice in choices %} | |
<option value="{{ choice }}"></option> | |
{% endfor %} | |
</datalist> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('country', 'datalist', [choices' => ['a', 'b']]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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