/** * @param Entity\Note $note * @param string $url * @param string $action * @param array $members * @return \Zend\Form\Form */ public function getNoteForm(Entity\Note $note, $url = '', $action = 'add', $members = null) { if (is_null($this->noteForm)) { $builder = new AnnotationBuilder($this->getEntityManager()); $this->noteForm = $builder->createForm($note); $this->noteForm->setAttribute('action', $url); $this->noteForm->setAttribute('id', 'noteForm'); $this->noteForm->setHydrator(new DoctrineObject($this->getEntityManager(), 'Secretary\\Entity\\Note')); $this->noteForm->bind($note); if ($action == 'edit' && $note->getPrivate() === false) { $this->noteForm->remove('private'); $group = $note->getGroup(); $membersString = $this->getMembersString(array_keys($members)); $this->noteForm->get('groupHidden')->setValue($group->getId()); $this->noteForm->get('members')->setValue($membersString); $this->noteForm->getInputFilter()->remove('__initializer__'); $this->noteForm->getInputFilter()->remove('__cloner__'); $this->noteForm->getInputFilter()->remove('__isInitialized__'); $this->noteForm->getInputFilter()->remove('lazyPropertiesDefaults'); } else { $this->noteForm->get('private')->setAttribute('required', false); $this->noteForm->getInputFilter()->get('private')->setRequired(false); } } return $this->noteForm; }
/** * Create service * * @param ServiceLocatorInterface $serviceLocator * @return mixed */ public function createService(ServiceLocatorInterface $serviceLocator) { $form = new Form(); $form->add(['name' => 'name', 'options' => ['label' => 'Name'], 'attributes' => ['type' => 'text']]); $form->add(['name' => 'description', 'options' => ['label' => 'Description'], 'attributes' => ['type' => 'textarea']]); $form->add(['name' => 'location', 'options' => ['label' => 'Location'], 'attributes' => ['type' => 'text']]); $form->setHydrator(new ClassMethods()); $form->setInputFilter($this->getInputFilter()); return $form; }
public function testPrepareBindDataAllowsFilterToConvertStringToArray() { $data = array('foo' => '1,2'); $filteredData = array('foo' => array(1, 2)); $element = new TestAsset\ElementWithStringToArrayFilter('foo'); $hydrator = $this->getMock('Zend\\Stdlib\\Hydrator\\ArraySerializable'); $hydrator->expects($this->any())->method('hydrate')->with($filteredData, $this->anything()); $this->form->add($element); $this->form->setHydrator($hydrator); $this->form->setObject(new stdClass()); $this->form->setData($data); $this->form->bindValues($data); }
public function testCorrectlyHydrateBaseFieldsetWhenHydratorThatDoesNotIgnoreInvalidDataIsUsed() { $fieldset = new Fieldset('example'); $fieldset->add(array('name' => 'foo')); // Add an hydrator that ignores if values does not exist in the $fieldset->setObject(new Entity\SimplePublicProperty()); $fieldset->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty()); $this->form->add($fieldset); $this->form->setBaseFieldset($fieldset); $this->form->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty()); // Add some inputs that do not belong to the base fieldset $this->form->add(array('type' => 'Zend\\Form\\Element\\Submit', 'name' => 'submit')); $object = new Entity\SimplePublicProperty(); $this->form->bind($object); $this->form->setData(array('submit' => 'Confirm', 'example' => array('foo' => 'value example'))); $this->assertTrue($this->form->isValid()); // Make sure the object was not hydrated at the "form level" $this->assertFalse(isset($object->submit)); }
public function testElementDirectlyInTheForm() { $element = $this->getMoneyFieldset(); $element->init(); $form = new Form(); $form->setHydrator(new ObjectProperty()); $form->setObject(new StdClass()); $form->add($element, ['name' => 'money']); $this->assertFalse($form->setData([])->isValid()); $this->assertFalse($form->setData(['money' => ['amount' => '123', 'currency' => '']])->isValid()); $this->assertFalse($form->setData(['money' => ['amount' => '', 'currency' => 'BRL']])->isValid()); $data = ['money' => ['amount' => '500.20', 'currency' => 'BRL']]; $form->setData($data); $this->assertTrue($form->isValid()); $amountValue = $form->get('money')->get('amount')->getValue(); $currencyValue = $form->get('money')->get('currency')->getValue(); $object = $form->getData()->money; $this->assertSame('500.20', $amountValue); $this->assertSame('BRL', $currencyValue); $this->assertInstanceOf(Money::class, $object); $this->assertSame(50020, $object->getAmount()); $this->assertSame('BRL', $object->getCurrency()->getName()); }
public function testNestedCollections() { // @see https://github.com/zendframework/zf2/issues/5640 $addressesFieldeset = new \ZendTest\Form\TestAsset\AddressFieldset(); $addressesFieldeset->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $form = new Form(); $form->setHydrator(new ObjectPropertyHydrator()); $form->add(array('name' => 'addresses', 'type' => 'Collection', 'options' => array('target_element' => $addressesFieldeset, 'count' => 2))); $data = array(array('number' => '0000000001', 'street' => 'street1'), array('number' => '0000000002', 'street' => 'street2')); $phone1 = new Phone(); $phone1->setNumber($data[0]['number']); $phone2 = new Phone(); $phone2->setNumber($data[1]['number']); $address1 = new Address(); $address1->setStreet($data[0]['street']); $address1->setPhones(array($phone1)); $address2 = new Address(); $address2->setStreet($data[1]['street']); $address2->setPhones(array($phone2)); $customer = new stdClass(); $customer->addresses = array($address1, $address2); $form->bind($customer); //test for object binding foreach ($form->get('addresses')->getFieldsets() as $_fieldset) { $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Address', $_fieldset->getObject()); foreach ($_fieldset->getFieldsets() as $_childFieldsetName => $_childFieldset) { switch ($_childFieldsetName) { case 'city': $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\City', $_childFieldset->getObject()); break; case 'phones': foreach ($_childFieldset->getFieldsets() as $_phoneFieldset) { $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Phone', $_phoneFieldset->getObject()); } break; } } } //test for correct extract and populate $index = 0; foreach ($form->get('addresses') as $_addresses) { $this->assertEquals($data[$index]['street'], $_addresses->get('street')->getValue()); //assuming data has just 1 phone entry foreach ($_addresses->get('phones') as $phone) { $this->assertEquals($data[$index]['number'], $phone->get('number')->getValue()); } $index++; } }
public function testDoNotCreateExtraFieldsetOnMultipleBind() { $form = new \Zend\Form\Form(); $this->productFieldset->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $form->add($this->productFieldset); $form->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty()); $product = new Product(); $categories = array(new \ZendTest\Form\TestAsset\Entity\Category(), new \ZendTest\Form\TestAsset\Entity\Category()); $product->setCategories($categories); $market = new \StdClass(); $market->product = $product; // this will pass the test $form->bind($market); $this->assertSame(count($categories), iterator_count($form->get('product')->get('categories')->getIterator())); // this won't pass, but must $form->bind($market); $this->assertSame(count($categories), iterator_count($form->get('product')->get('categories')->getIterator())); }
public function testCreatesNewObjectsIfSpecified() { $this->productFieldset->setUseAsBaseFieldset(true); $categories = $this->productFieldset->get('categories'); $categories->setOptions(array('create_new_objects' => true)); $form = new \Zend\Form\Form(); $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $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->assertNotSame($categories[0], $cat1); $this->assertNotSame($categories[1], $cat2); }