please me zend framework 2:)
i want create form collection of fieldsets using form element manager (absolutely in official documentation).
my formelementmanager configuration:
'form_elements' => array( 'factories' => array( 'admin\form\taskform' => function($sm) { $form = new taskform(); $doctrimeentitymanager = $sm->getservicelocator()->get('doctrine\orm\entitymanager'); $form -> setentitymanager($doctrimeentitymanager); $form -> init(); return $form; }, 'admin\form\taskbrandfieldset' => function($sm) { $doctrimeentitymanager = $sm->get('doctrine\orm\entitymanager'); $form = new taskbrandfieldset(); $form->setentitymanager($doctrimeentitymanager); return $form; }, ) ),
admin\form\taskform (only problem part):
namespace admin\form; use doctrine\orm\entitymanager; use zend\form\form; class taskform extends form { protected $entitymanager; public function init() { $this->setattribute('method', 'post'); // id $this->add(array( 'name' => 'id', 'attributes' => array( 'type' => 'hidden', ), )); // fieldset $this->add(array( 'type' => 'zend\form\element\collection', 'name' => 'taskbrands', 'options' => array( 'label' => 'brand of product', 'count' => 0, 'should_create_template' => true, 'allow_add' => true, 'target_element' => array( 'type'=>'admin\form\taskbrandfieldset' ), ), 'attributes' => array( 'id' => 'addressfieldset' ) )); } }
admin\form\taskbrandfieldset:
namespace admin\form; use admin\entity\taskbrand; use zend\form\fieldset; use zend\inputfilter\inputfilterproviderinterface; use zend\stdlib\hydrator\classmethods classmethodshydrator; use zend\servicemanager\servicelocatorawareinterface; use zend\servicemanager\servicelocatorinterface; class taskbrandfieldset extends fieldset implements inputfilterproviderinterface, servicelocatorawareinterface { protected $entitymanager; protected $servicelocator; public function init() { $this->setname('taskbrandfieldset'); $this->sethydrator(new classmethodshydrator(false)) ->setobject(new taskbrand()); $this->setlabel('taskbrand'); $this->add(array( 'type' => 'doctrinemodule\form\element\objectselect', 'name' => 'brand', 'options' => array( 'object_manager' => $this->getentitymanager(), 'target_class' => 'module\entity\brand', 'property' => 'name', ), )); } }
and, finally, controller:
$task = $this->getservicelocator()->get('admin\model\task')->findbypk($id); $formmanager = $this->getservicelocator()->get('formelementmanager'); $form = $formmanager->create('admin\form\taskform'); $form->bind($task);
the problem form admin\form\taskform instantiates in factory described in form_elements configuration section, admin\form\taskbrandfieldset not. invokes.
trying understand problem found admin\form\taskform , admin\form\taskbrandfieldset instantiates different instances of formelementmanager, first 1 have config inside (including factories description), second has nothing.
please me :)
the problem in controller. use
$form = $formmanager->get('admin\form\taskform');
instead of
$form = $formmanager->create('admin\form\taskform');
remember don't have use $form->init(). it's automatically called, same in zf1. there tutorial on zf2 site
Comments
Post a Comment