Symfony how to extend collection form field -
i'm trying extend collection form in order render own template...
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';   } } and use type in way:
$builder->add('contacts',new contactfieldtype(), array(   'label_attr' => array('class' => 'contacts') )); i'm getting following error:
the form's view data expected of type scalar, array or instance of \arrayaccess, instance of class myapp\mainbundle\entity\contact. can avoid error setting "data_class" option "myapp\mainbundle\entity\contact" or adding view transformer transforms instance of class myapp\mainbundle\entity\contact scalar, array or instance of \arrayaccess.
if use this:
$builder->add('contacts','collection', array(   'type' => new contacttype(),   'label_attr' => array('class' => 'contacts') )); it works fine.
i don't want implement data_class suggested...i extend collection widget
your form definition seems fine , not need more tinkering. problem lies in instanciation.
from see, believe instanciate form more or less way:
$model = new parentclass(); $model->setcontacts(new contact());  $form = $this->createform(new parentformtype(), $model)); however, collection form types not use contact() instance work array of such instances.
having fed instanciation contact() instance instead of array of such objects, code fails. need is:
$model = new parentclass(); $model->setcontacts(array(new contact()));  $form = $this->createform(new parentformtype(), $model)); although believe did, have not submitted instanciation code remains difficult tell if i'm right. if problem persists, suggest provide it.
Comments
Post a Comment