Checking the number of options in the Zend Multiselect form

I have a Multiselect Zend Form element with many parameters. I need to check the number of selected options (at least N options and no more than M options). I want the error message to be printed in the form, like a regular Zend validation error message.

What is the easiest (and least dangerous) way to do this?

A regular validator cannot do this because each selected value is checked individually.

I tried to override the formValid method and added logic there (return false and add an error message if the number is outside the valid range), but this leads to the error message being printed several times (for each selected value). I feel that trying to fix this will lead to extremely hacker code.

thanks for the help

+4
source share
2 answers

I decided to create my own Errors element decorator that discards non-standard error messages:

<?php class Element_Decorator_Errors extends Zend_Form_Decorator_Abstract { /** * Render errors * * @param string $content * @return string */ public function render($content) { $element = $this->getElement(); $view = $element->getView(); if (null === $view) { return $content; } // The array_unique is the only difference in comparison to the default Error decorator $errors = array_unique($element->getMessages()); if (empty($errors)) { return $content; } $separator = $this->getSeparator(); $placement = $this->getPlacement(); $errors = $view->formErrors($errors, $this->getOptions()); switch ($placement) { case self::APPEND: return $content . $separator . $errors; case self::PREPEND: return $errors . $separator . $content; } } } ?> 
0
source

I don't know if this is too much for you.

 $element = new Zend_Form_Element_Multiselect('CheckThis'); $options = array( 1 => 'Option One', 2 => 'Option Two', 3 => 'Option Three', 4 => 'Option Four', 5 => 'Option Five', 6 => 'Option Six', 7 => 'Option Seven', 8 => 'Option Eight', ); $element->addMultiOptions($options); $betweenOptions = array('min' => 2, 'max' => 4); $betweenValidator = new Zend_Validate_Between($betweenOptions); $betweenValidator->setMessage("The number of submitted values '%value%' is not between '%min%' and '%max%', inclusively",'notBetween'); if ( true === $this->getRequest()->isPost() ) { if ( true === $betweenValidator->isValid(count($_POST['CheckThis'])) ) { $form->isValid($_POST); } else { $messages = $betweenValidator->getMessages(); $element->addError($messages['notBetween']); $form->setDefaults($_POST); } } 

UPDATE
Note to avoid duplicate error messages.
If you cannot call isValid on a form or element; as in my example, where I only add an error message and set the default values. The problem is that isValid($value) will call _getErrorMessages() , and this method checks for error messages for values.

If you cannot avoid calling isValid , I would extend the Multiselect element and override this _ getErrorMessages() method with my one logic. This method can be found in the Zend/Form/Element.php class to the end.

+1
source

All Articles