Symfony2我的自定义表单字段类型未填充来自实体的数据

时间:2016-01-27 10:19:31

标签: forms symfony custom-field-type

我创建了简单的自定义字段

class InputWithDescriptionType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'description'         => '',
    ));

}

public function buildForm(FormBuilderInterface $builder, array $options)
{
   $builder
       ->setAttribute('description', $options['description'])
   ;
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
    $view->vars['description'] = $options['description'];
}

public function getParent()
{
    return 'text';
}

public function getName()
{
    return 'input_with_description';
}
}

并将其添加到我的表单

$builder
        ->add('name')
        ->add('about', 'input_with_description', array(
            'description' => "description",
            'label' => "label",
        ))

提交表单时,所有数据都已正确保存,但当我尝试使用此表单编辑实体时,自定义字段全部为空。问题出在哪里?

1 个答案:

答案 0 :(得分:1)

public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Product'
        ));
    }

替换' AppBundle \ Entity \ Product'与您的实体类。