Example #1
0
 public function __construct()
 {
     parent::__construct('create_product');
     $this->setAttribute('method', 'post')->setHydrator(new ClassMethodsHydrator())->setInputFilter(new InputFilter());
     $fieldset = new ProductFieldset();
     $fieldset->setUseAsBaseFieldset(true);
     $this->add($fieldset);
     $this->add(array('name' => 'submit', 'attributes' => array('type' => 'submit')));
 }
Example #2
0
 public function testCanBindObjectMultipleNestedFieldsets()
 {
     $productFieldset = new ProductFieldset();
     $productFieldset->setHydrator(new ClassMethods());
     $productFieldset->setObject(new Product());
     $nestedFieldset = new Fieldset('nested');
     $nestedFieldset->setHydrator(new ClassMethods());
     $nestedFieldset->setObject(new stdClass());
     $nestedFieldset->add(array('name' => 'products', 'type' => 'Collection', 'options' => array('target_element' => $productFieldset, 'count' => 2)));
     $mainFieldset = new Fieldset('main');
     $mainFieldset->setUseAsBaseFieldset(true);
     $mainFieldset->setHydrator(new ClassMethods());
     $mainFieldset->setObject(new stdClass());
     $mainFieldset->add(array('name' => 'nested', 'type' => 'Collection', 'options' => array('target_element' => $nestedFieldset, 'count' => 2)));
     $form = new Form();
     $form->setHydrator(new ObjectPropertyHydrator());
     $form->add($mainFieldset);
     $market = new stdClass();
     $prices = array(100, 200);
     $products[0] = new Product();
     $products[0]->setPrice($prices[0]);
     $products[1] = new Product();
     $products[1]->setPrice($prices[1]);
     $shop[0] = new stdClass();
     $shop[0]->products = $products;
     $shop[1] = new stdClass();
     $shop[1]->products = $products;
     $market->main = $shop;
     $form->bind($market);
     //test for object binding
     foreach ($form->get('main')->getFieldsets() as $_fieldset) {
         foreach ($_fieldset->getFieldsets() as $_nestedfieldset) {
             $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Product', $_nestedfieldset->get('products')->getObject());
         }
     }
 }
Example #3
0
 public function testCollectionCanBindObjectAndPopulateAndExtractNestedFieldsets()
 {
     $productFieldset = new \ZendTest\Form\TestAsset\ProductFieldset();
     $productFieldset->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
     $mainFieldset = new Fieldset();
     $mainFieldset->setObject(new stdClass());
     $mainFieldset->setHydrator(new ObjectPropertyHydrator());
     $mainFieldset->add($productFieldset);
     $form = new Form();
     $form->setHydrator(new ObjectPropertyHydrator());
     $form->add(array('name' => 'collection', 'type' => 'Collection', 'options' => array('target_element' => $mainFieldset, 'count' => 2)));
     $market = new stdClass();
     $prices = array(100, 200);
     $categoryNames = array('electronics', 'furniture');
     $productCountries = array('Russia', 'Jamaica');
     $shop1 = new stdClass();
     $shop1->product = new Product();
     $shop1->product->setPrice($prices[0]);
     $category = new \ZendTest\Form\TestAsset\Entity\Category();
     $category->setName($categoryNames[0]);
     $shop1->product->setCategories(array($category));
     $country = new \ZendTest\Form\TestAsset\Entity\Country();
     $country->setName($productCountries[0]);
     $shop1->product->setMadeInCountry($country);
     $shop2 = new stdClass();
     $shop2->product = new Product();
     $shop2->product->setPrice($prices[1]);
     $category = new \ZendTest\Form\TestAsset\Entity\Category();
     $category->setName($categoryNames[1]);
     $shop2->product->setCategories(array($category));
     $country = new \ZendTest\Form\TestAsset\Entity\Country();
     $country->setName($productCountries[1]);
     $shop2->product->setMadeInCountry($country);
     $market->collection = array($shop1, $shop2);
     $form->bind($market);
     //test for object binding
     $_marketCollection = $form->get('collection');
     $this->assertInstanceOf('Zend\\Form\\Element\\Collection', $_marketCollection);
     foreach ($_marketCollection as $_shopFieldset) {
         $this->assertInstanceOf('Zend\\Form\\Fieldset', $_shopFieldset);
         $this->assertInstanceOf('stdClass', $_shopFieldset->getObject());
         // test for collection -> fieldset
         $_productFieldset = $_shopFieldset->get('product');
         $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\ProductFieldset', $_productFieldset);
         $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Product', $_productFieldset->getObject());
         // test for collection -> fieldset -> fieldset
         $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\CountryFieldset', $_productFieldset->get('made_in_country'));
         $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Country', $_productFieldset->get('made_in_country')->getObject());
         // test for collection -> fieldset -> collection
         $_productCategories = $_productFieldset->get('categories');
         $this->assertInstanceOf('Zend\\Form\\Element\\Collection', $_productCategories);
         // test for collection -> fieldset -> collection -> fieldset
         foreach ($_productCategories as $_category) {
             $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\CategoryFieldset', $_category);
             $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Category', $_category->getObject());
         }
     }
     // test for correct extract and populate form values
     // test for collection -> fieldset -> field value
     foreach ($prices as $_k => $_price) {
         $this->assertEquals($_price, $form->get('collection')->get($_k)->get('product')->get('price')->getValue());
     }
     // test for collection -> fieldset -> fieldset ->field value
     foreach ($productCountries as $_k => $_countryName) {
         $this->assertEquals($_countryName, $form->get('collection')->get($_k)->get('product')->get('made_in_country')->get('name')->getValue());
     }
     // test collection -> fieldset -> collection -> fieldset -> field value
     foreach ($categoryNames as $_k => $_categoryName) {
         $this->assertEquals($_categoryName, $form->get('collection')->get($_k)->get('product')->get('categories')->get(0)->get('name')->getValue());
     }
 }