Exemplo n.º 1
0
 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 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());
 }
 /**
  * Hydrate an array of data onto an entity using a form
  *
  * @param AbstractEntity $entity
  * @param array $values
  * @param Form $form
  * @return AbstractEntity
  */
 private function hydrate(AbstractEntity $entity, array $values, Form $form)
 {
     $form->setObject($entity);
     if ($form->getBaseFieldset()) {
         $form->getBaseFieldset()->setObject($entity);
     }
     $form->setData($values);
     if (!$form->isValid()) {
         return $this->setErrorMessages($form->getMessages());
     }
     $result = $form->getData();
     if (!$result instanceof AbstractEntity) {
         throw new RuntimeException('Unable to retrieve entity from Form');
     }
     return $result;
 }
Exemplo n.º 4
0
 public function testFormAsFieldsetWillBindValuesToObject()
 {
     $parentForm = new Form('parent');
     $parentFormObject = new \ArrayObject(array('parentId' => null));
     $parentFormElement = new Element('parentId');
     $parentForm->setObject($parentFormObject);
     $parentForm->add($parentFormElement);
     $childForm = new Form('child');
     $childFormObject = new \ArrayObject(array('childId' => null));
     $childFormElement = new Element('childId');
     $childForm->setObject($childFormObject);
     $childForm->add($childFormElement);
     $parentForm->add($childForm);
     $data = array('parentId' => 'mpinkston was here', 'child' => array('childId' => 'testing 123'));
     $parentForm->setData($data);
     $this->assertTrue($parentForm->isValid());
     $this->assertEquals($data['parentId'], $parentFormObject['parentId']);
     $this->assertEquals($data['child']['childId'], $childFormObject['childId']);
 }