Symfony 4自定义表单类型

时间:2019-04-18 13:21:48

标签: symfony symfony4

我正在尝试使用自定义类型表单,但无法呈现它(完全没有错误,只是一个空的下拉列表)。自定义类型为:

class RuleType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
           'choices' => [
               '1' => 'Regle',
               '2' => 'Scenario'
           ],
        ]);
    }



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


}   

,它用于:

  public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name',        TextType::class, array('attr' => array('class' => 'long')))
        ->add('ruleType',    RuleType::class)
        ->add('isActive',    CheckboxType::class, array('required' => false))
        ->add('iconName',    Image::class, array('required' => false))
        ->add('action',        EntityType::class, array('class' => 'App\Entity\Upv6\Actions', 'choice_label' => 'internalName'))
        ->add('rule',        TextareaType::class, array('required' => false, 'attr' => array('class' => 'editor')))
        ->add('comment',    TextareaType::class, array('required' => false, 'attr' => array('class' => 'comments')))
        ;
}

和模板:

{{ form_start(form, {'attr': {'id': 'monForm'},'action': path('iot6_SmartItBundle_RulesManager_Add') }) }}
    {{ form_errors(form) }}
    <table>
        <tr>
            <td>{{ form_label(form.name, 'msg.name'|trans) }}</td>
            <td>{{ form_widget(form.name) }}</td>
            <td>{{ form_errors(form.name) }}</td>
        </tr>
        <tr>

            <td>{{ form_label(form.ruleType, 'msg.type'|trans) }}</td>
            <td>{{ form_widget(form.ruleType) }}</td>
            <td>{{ form_errors(form.ruleType) }}</td>
        </tr>

1 个答案:

答案 0 :(得分:1)

public function setDefaultOptions(OptionsResolverInterface $resolver)

应该是

public function configureOptions(OptionsResolver $resolver)

我的猜测是,您正在查看一些2.7之前的示例代码,就像setDefaultOptionsdeprecated一样。您可以找到latest docs for custom form types here