Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6576341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:30:01+00:00 2026-05-25T15:30:01+00:00

I have an Multiselect Zend Form element with many options. I have to validate

  • 0

I have an Multiselect Zend Form element with many options. I have to validate the number of selected options (at least N options and at most M options are selected). I want the error message to be printed in the form just like regular Zend Validate error message.

What is the simplest (and the least hacky) way to do this?

A regular validator can’t do this, because each selected value is validated completely individually.

I tried to override the form’s isValid method and added the logic there (return false and add an error message if the number is outside the allowed range), but that results in the error message being printed multiple times (for each selected value). I feel that trying to fix this would result in extremely hacky code.

Thanks for help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:30:02+00:00Added an answer on May 25, 2026 at 3:30 pm

    I decided to create my custom Errors element decorator, which discards non-unique 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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multiselect listbox and before submitting the form i want to check
i have a multiselect box (of interests) in my zend form, and i am
I have a form with a multiselect field with several options, which looks like
In my Zend Framework project, I have a form that I am testing. In
I have a ListBox in a WinForm with multiselect enabled. The selected items appear
I have a windows form with a listview control. I set the MultiSelect property
I have a multiselect box, and want to change the default blue color that
I have a listbox in a Microsoft Access form. The MultiSelect property is set
I'm having problems with getting items in a multiselect to get selected. I have
I have a problem with posting a multiselect, I get error: There is no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.