public function testCanSetDescription() { $this->testDescriptionInitiallyNull(); $description = "This is a description"; $this->form->setDescription($description); $this->assertEquals($description, $this->form->getDescription()); }
/** * Returns a Form builds from widget's form definition or instanciated from given classname * * Form definition: * * A widget can define one or more Form in its xml description file. A Form can be used, either as main output of * the widget, or on the dashboard screen for setting purpose. When you pass a Form name as argument, this method * will try to retrieve the associated Form definition and build the Form from it. * * Form class: * * It's also possible to pass an associative array where the key is a Form classname and the value, the path where * to find the file that contains the class. It this case, An instance of the class will be created and returned. * * Common treatment: * * In all cases, if the Form's target is the dashboard screen, a specific hidden field is added to it. Also, if the * Form has not submit element, it's automatically generated and added to it. * * Alternative way: * * The alternative way is to override this method in subclass and build your Form into it directly. Note that the * {@link iPMS_Widget::getForm()} method is used both to retrieve Form for main output of the widget, and the * dashboard screen. * * @throws iPMS_Widgets_Exception if form definition was not found * @throws iPMS_Widgets_Exception if form class was not found * @param string|array $form Either a Form name to build from a Form's definition, or an array where the key is the * Form classname and the value, the path where to find it * @return Zend_Form a Zend_Form object */ public function getForm($form) { if (is_string($form)) { // Form generated from Form definition if (isset($this->_formInstances[$form])) { return $this->_formInstances[$form]; } $formDef = $this->getFormDefinition($form); if (!array_key_exists('type', $formDef['element'])) { $formDef = $formDef['element']; } $elementStack = array(); foreach ($formDef as $elementDef) { /** * @var $element Zend_Form_Element|Zend_Form_Element_Select */ $className = 'Zend_Form_Element_' . ucfirst($elementDef['type']); $element = new $className($elementDef['name'], array('label' => isset($elementDef['label']) ? $elementDef['label'] : '', 'value' => isset($elementDef['val']) ? $elementDef['val'] : '', 'helper' => 'form' . ucfirst($elementDef['type']), 'required' => isset($elementDef['required']) ? (int) $elementDef['required'] : 0)); if ($elementDef['type'] == 'select') { if (isset($elementDef['option'])) { foreach ($elementDef['option'] as $option) { $element->addMultiOptions(array($option['val'] => $option['label'])); if (isset($option['selected'])) { $element->setValue($option['val']); } } } elseif (isset($elementDef['callback'])) { if (isset($elementDef['callback']['function'])) { $options = call_user_func_array($elementDef['callback']['function'], (array) isset($elementDef['callback']['argument']) ? $elementDef['callback']['argument'] : array()); if ($elementDef['callback']['function'] == 'range') { $options = array_combine($options, $options); } $element->addMultiOptions($options); if (isset($elementDef['callback']['selected'])) { $element->setValue($elementDef['callback']['selected']); } } else { require_once 'iPMS/Widgets/Exception.php'; throw new iPMS_Widgets_Exception('Unable to generate element options from callback: function name not found'); } } else { require_once 'iPMS/Widgets/Exception.php'; throw new iPMS_Widgets_Exception('All select elements must have options'); } } if (isset($elementDef['id'])) { // Optional CSS id for element $element->setAttrib('id', $elementDef['id']); } if (isset($elementDef['class'])) { // Optional CSS class for element $element->setAttrib('class', $elementDef['class']); } if (isset($elementDef['filter'])) { // Optional filter(s) for element $element->addFilters((array) $elementDef['filter']); } // Optional validator for element if (isset($elementDef['validator'])) { // Optional validator for element $element->addValidators((array) $elementDef['validator']); } $elementStack[] = $element; } // Creating new form instance $formInstance = new Zend_Form(); $formInstance->setName($form)->setElementsBelongTo($form)->addElements($elementStack); // Optional description for the Form if (isset($formDef['description'])) { $formInstance->setDescription($formDef['description'])->addDecorator('Description', array('placement' => 'prepend')); } } elseif (is_array($form) && isset($form['clasname'])) { // Form generated from specific class $className = $form['clasname']; if (isset($this->_formInstances[$className])) { return $this->_formInstances[$className]; } if (!class_exists($className)) { require_once 'Zend/Loader.php'; Zend_Loader::loadClass($className, isset($form['path']) ? isset($form['path']) : null); } $formInstance = new $className(); if (!$formInstance instanceof Zend_Form) { require_once 'iPMS/Widgets/Exception.php'; throw new iPMS_Widgets_Exception(sprintf('Invalid argument: Detected class "%s", which is not an instance of Zend_Form', $className)); } } else { require_once 'iPMS/Widgets/Exception.php'; throw new iPMS_Widgets_Exception('Invalid argument $form'); } // Common treatment if ($form !== 'dashboard') { $hidden = new Zend_Form_Element_Hidden('widgetUpdate'); $hidden->setValue($this . '_' . $this->_id)->setRequired(true); $hidden->getDecorator('HtmlTag')->setOption('class', 'hidden'); $hidden->getDecorator('Label')->setOption('tagClass', 'hidden'); $formInstance->addElement($hidden); } // Add a submit element if it doesn't already defined if (null === $formInstance->getElement('submit')) { $submit = new Zend_Form_Element_Submit('submit'); $submit->setLabel('submit'); $formInstance->addElement($submit); } return $this->_formInstances[isset($className) ? $className : $form] = $formInstance; }
<?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();
<?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 belongsTo * for shipping/billing addresses. */ $form = new Zend_Form(); $form->setView($view); $form->addDecorator('Description', array('escape' => false, 'placement' => 'PREPEND')); $form->setDescription('<h3>Using belongsTo</h3>'); $form->addElement('text', 'recipient', array('label' => 'Ship to', 'required' => true, 'belongsTo' => 'shipping')); $form->addElement('text', 'address1', array('label' => 'Address 1', 'required' => true, 'belongsTo' => 'shipping[addresses]')); $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();