我想爲了與自己的模板,以使其延長採集表...的Symfony如何延長收集表單字段
class ContactFieldType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'collection' => array('type' => new ContactType())
));
}
public function getParent()
{
return 'collection';
}
public function getName()
{
return 'contactField';
}
}
我以這種方式使用此類型:
$builder->add('contacts',new ContactFieldType(), array(
'label_attr' => array('class' => 'contacts')
));
,我發現了以下錯誤:
The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class MyApp\MainBundle\Entity\Contact. You can avoid this error by setting the "data_class" option to "MyApp\MainBundle\Entity\Contact" or by adding a view transformer that transforms an instance of class MyApp\MainBundle\Entity\Contact to scalar, array or an instance of \ArrayAccess.
,如果我用這個:
$builder->add('contacts','collection', array(
'type' => new ContactType(),
'label_attr' => array('class' => 'contacts')
));
它工作正常。
我不想按照建議執行這一data_class ...我想延長收集部件