public function addSubForm(Zend_Form $form, $name, $order = null) { $form->loadDefaultDecorators(); $form->removeDecorator('Form'); $form->addDecorator('Fieldset')->addDecorator('DtDdWrapper'); $form->setElementsBelongTo($name); return parent::addSubForm($form, $name, $order); }
public function testLabelIdIsCorrect() { $form = new \Zend_Form(); $form->setElementsBelongTo('comment'); $this->element->setLabel("My Captcha"); $form->addElement($this->element); $html = $form->render($this->getView()); $expect = sprintf('for="comment-%s-input"', $this->element->getName()); $this->assertRegexp("/<label [^>]*?{$expect}/", $html, $html); }
public function testElementsRenderAsSubArrayMembersWhenElementsBelongToASubArray() { $this->setupElements(); $this->form->setElementsBelongTo('data[foo]'); $html = $this->form->render($this->getView()); $this->assertContains('name="data[foo][foo]"', $html); $this->assertContains('name="data[foo][bar]"', $html); $this->assertContains('name="data[foo][baz]"', $html); $this->assertContains('id="data-foo-foo"', $html); $this->assertContains('id="data-foo-bar"', $html); $this->assertContains('id="data-foo-baz"', $html); }
/** * Set element value * * @param mixed $value * @return \MUtil_Form_Element_Table (continuation pattern) */ public function setValue($value) { // $this->setElementsBelongTo($this->getName()); if ($this->_subForm && $value) { $this->_subForm->setElementsBelongTo($this->getName()); foreach ($value as $id => $row) { if (isset($this->_subForms[$id])) { $this->_subForms[$id]->populate($row); } else { $subForm = clone $this->_subForm; $name = $this->getName() . '[' . $id . ']'; $subForm->setElementsBelongTo($name); $subForm->setName($name); $subForm->populate($row); $this->_subForms[$id] = $subForm; } } } return parent::setValue($value); }
/** * Add a form group/subform * * @param Zend_Form $form * @param string $name * @param int $order * @return Zend_Form */ public function addSubForm(Zend_Form $form, $name, $order = null) { $name = (string) $name; /** @var Zend_Loader_PluginLoader $loader */ 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; }
public function testCanValidatePartialNestedFormsWithElementsBelongingToArrays() { $form = new Zend_Form(); $form->setElementsBelongTo('foobar'); $form->addElement('text', 'firstName') ->getElement('firstName') ->setRequired(false); $form->addElement('text', 'lastName') ->getElement('lastName') ->setRequired(true); $subForm = new Zend_Form_SubForm(); $subForm->setElementsBelongTo('foobar[baz]'); $subForm->addElement('text', 'email') ->getElement('email') ->setRequired(true) ->addValidator('NotEmpty'); $subSubForm = new Zend_Form_SubForm(); $subSubForm->setElementsBelongTo('foobar[baz][bat]'); $subSubForm->addElement('checkbox', 'home') ->getElement('home') ->setRequired(true) ->addValidator('NotEmpty'); $subForm->addSubForm($subSubForm, 'subSub'); $form->addSubForm($subForm, 'sub') ->addElement('submit', 'save', array('value' => 'submit')); $data = array('foobar' => array( 'lastName' => 'Cow', )); $this->assertTrue($form->isValidPartial($data)); $this->assertEquals('Cow', $form->lastName->getValue()); $firstName = $form->firstName->getValue(); $email = $form->sub->email->getValue(); $home = $form->sub->subSub->home->getValue(); $this->assertTrue(empty($firstName)); $this->assertTrue(empty($email)); $this->assertTrue(empty($home)); $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6)); $data['foobar']['baz'] = array('bat' => array('home' => 'ab')); $this->assertFalse($form->isValidPartial($data), var_export($form->sub->subSub->home, 1)); $this->assertEquals('1', $form->sub->subSub->home->getValue()); $messages = $form->getMessages(); $this->assertFalse(empty($messages)); $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']), var_export($messages, 1)); $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']['stringLengthTooShort'])); }
/** * Возвращает массив объектов Zend_Form мини-форм по типам * * @return array */ public function getForms() { $forms = array(); foreach ($this->configs as $fieldtype => $template) { $form = new Zend_Form($template); $form->setElementsBelongTo('fields[]'); $form->addDisplayGroup(array_keys($template['elements']), 'main', array('legend' => $template['label'], 'class' => 'ui-widget-content')); $form->removeDecorator('Form'); $form->removeDecorator('DtDdWrapper'); $form->setTranslator($this->translate); $forms[$fieldtype] = $form; } return $forms; }
public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays() { $this->_checkZf2794(); $form = new Zend_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)); }
<?php ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/path/to/zend/library'); require_once 'Zend/Loader.php'; Zend_Loader::registerAutoload(); $request = new Zend_Controller_Request_Http(); $view = new Zend_View(); /** * Example showing usage of array notation using setElementsBelongTo * on the form level, for shipping/billing addresses. */ $form = new Zend_Form(); $form->setView($view); $form->addDecorator('Description', array('escape' => false, 'placement' => 'PREPEND')); $form->setDescription('<h3>Using setElementsBelongTo</h3>'); $form->setElementsBelongTo('shipping'); $form->addElement('text', 'recipient', array('label' => 'Ship to', 'required' => true)); $form->addElement('text', 'address', array('label' => 'Address', 'required' => true)); $form->addElement('submit', 'submit', array('label' => 'Submit')); if ($request->isPost()) { if ($form->isValid($request->getPost())) { echo 'Order placed, Thank you!'; } else { echo 'You have errors in your form, please check'; } } echo $form->render();