Example #1
0
File: Form.php Project: rikaix/zf2
 /**
  * Add a form group/subform
  *
  * @param  Form $form
  * @param  string $name
  * @param  int $order
  * @return Form
  */
 public function addSubForm(Form $form, $name, $order = null)
 {
     $name = (string) $name;
     foreach ($this->_loaders as $type => $loader) {
         $loaderPaths = $loader->getPaths();
         foreach ($loaderPaths as $prefix => $paths) {
             foreach ($paths as $path) {
                 $form->addPrefixPath($prefix, $path, $type);
             }
         }
     }
     if (!empty($this->_elementPrefixPaths)) {
         foreach ($this->_elementPrefixPaths as $spec) {
             list($prefix, $path, $type) = array_values($spec);
             $form->addElementPrefixPath($prefix, $path, $type);
         }
     }
     if (!empty($this->_displayGroupPrefixPaths)) {
         foreach ($this->_displayGroupPrefixPaths as $spec) {
             list($prefix, $path) = array_values($spec);
             $form->addDisplayGroupPrefixPath($prefix, $path);
         }
     }
     if (null !== $order) {
         $form->setOrder($order);
     }
     if (($oldName = $form->getName()) && $oldName !== $name && $oldName === $form->getElementsBelongTo()) {
         $form->setElementsBelongTo($name);
     }
     $form->setName($name);
     $this->_subForms[$name] = $form;
     $this->_order[$name] = $order;
     $this->_orderUpdated = true;
     return $this;
 }
Example #2
0
 public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays()
 {
     $form = new Form();
     $form->setElementsBelongTo('foo[bar]');
     $form->addElement('text', 'firstName')->getElement('firstName')->setRequired(false);
     $form->addElement('text', 'lastName')->getElement('lastName')->setRequired(true);
     $subForm = new \Zend\Form\SubForm();
     $subForm->setElementsBelongTo('baz');
     $subForm->addElement('text', 'email')->getElement('email')->setRequired(true)->addValidator('NotEmpty');
     $subSubForm = new \Zend\Form\SubForm();
     $subSubForm->setElementsBelongTo('bat[quux]');
     $subSubForm->addElement('checkbox', 'home')->getElement('home')->setRequired(true)->addValidator('InArray', false, array(array('1')));
     $subForm->addSubForm($subSubForm, 'subSub');
     $form->addSubForm($subForm, 'sub')->addElement('submit', 'save', array('value' => 'submit'));
     $data = array('foo' => array('bar' => array('lastName' => 'Cow')));
     $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
     $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
     $form->isValidPartial($data);
     $messages = $form->getMessages();
     $this->assertFalse(empty($messages));
     $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1));
     $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1));
 }