php - Symfony2 From Collection has no elements -
i'm trying implement form collections. have members , each member can have multiple memberships. given member object "createform()" has array memberships correctly fetched database. member form gets shown correct, seems form.memberships empty. doesn't render "test".
controller
public function memberaction($id) { $request = request::createfromglobals(); $user_provider = $this->get('webservice_user_provider'); if ($id === "new") { $member = new webserviceuser(); $name = "neues mitlgied"; } else if (intval($id) != 0) { $member = $user_provider->getuserbyid(intval($id)); $member->getmemberships(); $name = $member->getname() . " " . $member->getlastname(); } else { throw new notfoundhttpexception('no member found!'); } $form = $this->createform(new membertype($this->get('webservice_config_provider')), $member); $form->handlerequest($request); if ($form->isvalid()) { return $this->redirect($this->generateurl('bow_exciting_fit_member', array('id' => $user_provider->persistmember($form->getviewdata())))); } return $this->render('bowexcitingfitbundle:default:member.html.twig', array( "member" => $member, "form" => $form->createview() )); }
membertype
class membertype extends abstracttype { function __construct(\bow\excitingfitbundle\webservice\webserviceconfigprovider $config) { $this->config = $config; } public function buildform(formbuilderinterface $builder, array $options) { ... more code ... $builder->add('memberships', 'collection', array( 'type' => new membershiptype($this->config), 'by_reference' => false, 'allow_add' => true)); ... more code ... } public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'bow\excitingfitbundle\webservice\webserviceuser', )); } public function getname() { return 'member'; } }
membershiptype
class membershiptype extends abstracttype { function __construct(\bow\excitingfitbundle\webservice\webserviceconfigprovider $config) { $this->config = $config; } public function buildform(formbuilderinterface $builder, array $options) { ... more code ... $builder->add('id', 'hidden'); $builder->add('user', 'hidden'); ... more code ... } public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'bow\excitingfitbundle\webservice\webservicemembership', )); } public function getname() { return 'membership'; } }
template
{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }} <div class="ui divided list memberships" data-prototype="{{ include('bowexcitingfitbundle:form:membership-prototype.html.twig', { 'form': form.memberships.vars.prototype })|e }}"> {% membership in form.memberships %} test {{ include('bowexcitingfitbundle:form:membership-prototype.html.twig', { 'form': membership }) }} {% endfor %} </div> {{ form_end(form) }}
even if don't use doctrine, had use arraycollection() instead of array memberships.
Comments
Post a Comment