From: <gem...@li...> - 2013-03-21 10:59:45
|
Revision: 1199 http://sourceforge.net/p/gemstracker/code/1199 Author: mennodekker Date: 2013-03-21 10:59:41 +0000 (Thu, 21 Mar 2013) Log Message: ----------- Allow more verbose error display for tabforms, disable by using $form->setVerbose(false) Modified Paths: -------------- trunk/library/classes/Gems/Form/Decorator/TabErrors.php trunk/library/classes/Gems/TabForm.php Modified: trunk/library/classes/Gems/Form/Decorator/TabErrors.php =================================================================== --- trunk/library/classes/Gems/Form/Decorator/TabErrors.php 2013-03-20 07:35:34 UTC (rev 1198) +++ trunk/library/classes/Gems/Form/Decorator/TabErrors.php 2013-03-21 10:59:41 UTC (rev 1199) @@ -3,7 +3,7 @@ /** * Copyright (c) 2011, Erasmus MC * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright @@ -14,7 +14,7 @@ * * Neither the name of Erasmus MC nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -25,7 +25,7 @@ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * @package Gems * @subpackage Form * @copyright Copyright (c) 2011 Erasmus MC @@ -44,6 +44,13 @@ class Gems_Form_Decorator_TabErrors extends Zend_Form_Decorator_Abstract { /** + * By default, show verbose error messages in tabforms + * + * @var boolean + */ + protected $_verbose = true; + + /** * Render the TabErrors * * We don't return anything, we just add a class to the tab so it shows the errors @@ -63,6 +70,23 @@ } /** + * Should the tab errors be verbose? + * + * Verbose means that apart from marking and selecting the tab that has errors + * we also show an error above the form. + * + * @return boolean + */ + public function getVerbose() { + if (null !== ($verboseOpt = $this->getOption('verbose'))) { + $this->_verbose = (bool) $verboseOpt; + $this->removeOption('verbose'); + } + + return $this->_verbose; + } + + /** * Recurse through a form object, rendering errors * * @param Zend_Form $form @@ -71,21 +95,62 @@ */ protected function _recurseForm(Zend_Form $form) { + $subFormsWithErrors = array(); + $subFormMessages = array(); $tabId = 0; - foreach ($form->getSubForms() as $subitem) { - if ($subitem instanceof Gems_Form_TabSubForm) { - //This is where we want to do something - foreach ($subitem->getElements() as $tabElement) { - $messages = $tabElement->getMessages(); - if (count($messages)) { - $subitem->setAttrib('jQueryParams', array('class'=>'taberror')); - $errors[$tabId] = $tabId; - $form->selectTab($tabId); - break; + + foreach ($form->getSubForms() as $subForm) { + if ($subForm instanceof Gems_Form_TabSubForm) { + // See if any of the subformelements has an error message + foreach ($subForm->getElements() as $subFormElement) { + $elementMessages = $subFormElement->getMessages(); + if (count($elementMessages)) { + $subFormsWithErrors[$tabId] = $subForm->getAttrib('title'); // Save subform title + $subForm->setAttrib('jQueryParams', array('class'=>'taberror')); // Add css class to the subform + $form->selectTab($tabId); // Select the tab, this way the last tab with error is always selected + break; // don't check other elements + } } + + // Preserve subform level custom messages if we have an error + if (array_key_exists($tabId, $subFormsWithErrors)) { + $subFormMessages[$tabId] = $subForm->getCustomMessages(); + } $tabId++; } + + // If we found at least one error, and 'verbose' is true + if ($this->getVerbose() && (!empty($subFormsWithErrors) || $form->isErrors()) ) { + // First show form level custom error messages (the elements show their own errors) + $formMessage = $form->getCustomMessages(); + if(!empty($formMessage)) { + foreach($formMessage as $message) + { + Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->addMessage($message); + } + } + + // Now browse through the tabs with errors + foreach ($subFormsWithErrors as $tabIdx => $tabName) + { + // If more then one tab, show in which tab we found the errors + if ($tabId > 1) { + $translator = Zend_Registry::get('Zend_Translate'); + Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->addMessage(sprintf($translator->_('Error in tab "%s"'), $tabName)); + } + + // If we have them, show the tab custom error messages + foreach ($subFormMessages[$tabIdx] as $subFormMessage) + { + foreach ($subFormMessage as $message) + { + Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->addMessage("--> " . $message); + } + } + } + + } } } } \ No newline at end of file Modified: trunk/library/classes/Gems/TabForm.php =================================================================== --- trunk/library/classes/Gems/TabForm.php 2013-03-20 07:35:34 UTC (rev 1198) +++ trunk/library/classes/Gems/TabForm.php 2013-03-21 10:59:41 UTC (rev 1199) @@ -299,6 +299,20 @@ $this->getElement('tab')->setValue($tabIdx); $this->setAttrib('selected', $tabIdx); } + + /** + * Set the form to be verbose, showing above the form what tabs have errors and + * possibly add custom (sub)formlevel error messages + * + * @param boolean $bool + */ + public function setVerbose($bool) + { + $decorator = $this->getDecorator('TabErrors'); + if ($decorator) { + $decorator->setOption('verbose', (bool) $bool); + } + } /** * Set the view object This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |