/** * Create admin user form * * Method to create the Doctrine ORM user form for edit/create users * * @return Zend\Form\Form */ public function createUserForm($userEntity, $formName = 'LogIn') { $entityManager = $this->getEntityManager(); $builder = new DoctrineAnnotationBuilder($entityManager); $this->form = $builder->createForm($userEntity); $this->form->setHydrator(new DoctrineHydrator($entityManager)); $this->form->setAttribute('method', 'post'); $this->addCommonFields(); switch ($formName) { case 'SignUp': $this->addSignUpFields(); $this->addSignUpFilters(); $this->form->setAttributes(array('action' => $this->getUrlPlugin()->fromRoute('user-register'), 'name' => 'register')); break; case 'EditProfile': $this->form->setAttributes(array('action' => $this->getUrlPlugin()->fromRoute('user-register', array('action' => 'edit-profile')), 'name' => 'edit-profile')); break; case 'ChangePassword': $this->addChangePasswordFields(); $this->addChangePasswordFilters(); $this->form->setAttributes(array('action' => $this->getUrlPlugin()->fromRoute('user-register', array('action' => 'change-password')), 'name' => 'change-password')); break; case 'ResetPassword': $this->addResetPasswordFields(); $this->addResetPasswordFilters(); $this->form->setAttributes(array('action' => $this->getUrlPlugin()->fromRoute('user-register', array('action' => 'reset-password')), 'name' => 'reset-password')); break; case 'ChangeEmail': $this->addChangeEmailFields(); $this->addChangeEmailFilters(); $this->form->setAttributes(array('action' => $this->getUrlPlugin()->fromRoute('user-register', array('action' => 'change-email')), 'name' => 'change-email')); break; case 'ChangeSecurityQuestion': $this->form->setAttributes(array('action' => $this->getUrlPlugin()->fromRoute('user-register', array('action' => 'change-security-question')), 'name' => 'change-security-question')); break; case 'CreateUser': $this->addCreateUserFields(); $this->addCreateUserFilters(); $this->form->setAttributes(array('action' => $this->getUrlPlugin()->fromRoute('user-admin', array('action' => 'create-user')), 'name' => 'register')); break; case 'EditUser': $this->form->setAttributes(array('name' => 'register')); break; default: $this->addLoginFields(); $this->addLoginFilters(); $this->form->setAttributes(array('action' => $this->getUrlPlugin()->fromRoute('user-index', array('action' => 'login')), 'name' => 'login')); break; } $this->form->bind($userEntity); return $this->form; }
public function testBindingWorksAsExpected() { $form = new \Zend\Form\Form(); $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $fieldset = new MoneyFieldset(); $fieldset->init(); $form->add($fieldset, array('name' => 'money')); $model = new TestModel(); $form->bind($model); $this->assertEquals(5432.1, $fieldset->get('amount')->getValue()); $this->assertEquals('ZAR', $fieldset->get('currency')->getValue()); $form->setData(array('money' => array('amount' => 1234.56, 'currency' => 'GBP'))); $this->assertTrue($form->isValid()); $bound = $form->getData(); $this->assertInstanceOf('NetglueMoney\\Form\\TestModel', $bound); $this->assertInstanceOf('NetglueMoney\\Money\\Money', $bound->money); $this->assertSame(123456, $bound->money->getAmount()); $this->assertSame('GBP', $bound->money->getCurrencyCode()); }
public function testExtractFromObjectDoesntTouchOriginalObject() { $form = new \Zend\Form\Form(); $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $this->productFieldset->setUseAsBaseFieldset(true); $form->add($this->productFieldset); $originalObjectHash = spl_object_hash($this->productFieldset->get("categories")->getTargetElement()->getObject()); $product = new Product(); $product->setName("foo"); $product->setPrice(42); $cat1 = new \ZendTest\Form\TestAsset\Entity\Category(); $cat1->setName("bar"); $cat2 = new \ZendTest\Form\TestAsset\Entity\Category(); $cat2->setName("bar2"); $product->setCategories(array($cat1, $cat2)); $form->bind($product); $form->setData(array("product" => array("name" => "franz", "price" => 13, "categories" => array(array("name" => "sepp"), array("name" => "herbert"))))); $objectAfterExtractHash = spl_object_hash($this->productFieldset->get("categories")->getTargetElement()->getObject()); $this->assertSame($originalObjectHash, $objectAfterExtractHash); }
public function testDoesNotCreateNewObjects() { $form = new \Zend\Form\Form(); $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $this->productFieldset->setUseAsBaseFieldset(true); $form->add($this->productFieldset); $product = new Product(); $product->setName("foo"); $product->setPrice(42); $cat1 = new \ZendTest\Form\TestAsset\Entity\Category(); $cat1->setName("bar"); $cat2 = new \ZendTest\Form\TestAsset\Entity\Category(); $cat2->setName("bar2"); $product->setCategories(array($cat1,$cat2)); $form->bind($product); $form->setData( array("product"=> array( "name" => "franz", "price" => 13, "categories" => array( array("name" => "sepp"), array("name" => "herbert") ) ) ) ); $form->isValid(); $categories = $product->getCategories(); $this->assertSame($categories[0], $cat1); $this->assertSame($categories[1], $cat2); }