如何创建Symfony 3.4 HTML数据列表表单字段

时间:2018-12-06 12:00:56

标签: forms datalist data-transfer symfony-3.4

我正在尝试用很多输入字段替换选择项

我在做下面的事情

我的数据列表类型:

namespace Epita\HousingBundle\Form\Type;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Epita\HousingBundle\Form\Type\BedroomsTransformer;

class DatalistType extends AbstractType
{
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options) {
    $builder->addModelTransformer(new BedroomsTransformer(), true);
}

public function getParent()
{
    return EntityType::class;
}

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';
}
}   

我有一个引用编号形式的字段,如下所示,并且我正在使用DatalistType::class如下所示

BedroomFormType:

namespace Epita\HousingBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Epita\HousingBundle\Entity\Bedroom;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;
use Epita\HousingBundle\Form\Type\DatalistType;

class BedroomFormType extends AbstractType {

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

    $builder->add('description', TextType::class, array(
    ));

    $builder->add('referencenumber', DatalistType::class,
                array('required' => true,
                'label' => 'Reference Number',
                'class' => 'EpitaHousingBundle:Bedroom',
                'choice_label' => 'referencenumber'   
                ));
public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => Bedroom::class,
        'csrf_protection' => false,
    ));
}
}    

我正在使用数据转换器来转换如下所示字段的数据

namespace Epita\HousingBundle\Form\Type;

use Symfony\Component\Form\DataTransformerInterface;
use Epita\HousingBundle\Entity\Bedroom;

class BedroomsTransformer implements DataTransformerInterface {

public function transform($value) {
    // transform the array to a string
//      return implode(', ', $value);
       return $value;  
}
public function reverseTransform($value) {
    // transform the string back to an array
//      return explode(', ', $value);
    return $value;
}
}

fields.html.twig:

{% block datalist_widget %}
    <input list="{{ id }}_list" {{ block('widget_attributes') }}>
    <datalist id="{{ id }}_list">
        {% for choice in choices %}
            <option value="{{ choice.label }}"></option>
        {% endfor %}
    </datalist>
{% endblock %}

services.yml:

form.type.datalist_type:
    class: Epita\HousingBundle\Form\Type\DatalistType
    tags:
        - { name: form.type, alias: datalist }

这里的问题是我已经嵌入了原型表单,并且数据列表将多余的(数据库参考号)参考号返回到下拉列表

请帮助我任何人。预先感谢...

0 个答案:

没有答案