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 7888983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:58:44+00:00 2026-06-03T05:58:44+00:00

I’m looking to set a single error message for a form, but still validate

  • 0

I’m looking to set a single error message for a form, but still validate each field.

The form is a survey with 10 questions. Each of them validates the same way (->setRequired(true)). So basically, I just to validate that each questions has an answer and display one message at the top of the form if one of them is not answered.

I’ve tried several solutions and gotten the same result. My error is added to the form, but also, all of the individual errors show as well.

This is my latest shot at it:

public function isValid($data)
{
    $errors = 0;

    parent::isValid($data);

    foreach ($this->getElements() as $element) {

        if ($element->hasErrors()) {

            $element->clearErrorMessages();

            $element->setErrors(array());

            $errors++;
        }
    }

    if (count($errors) > 0) {
        $this->setErrorMessages(array('Please answer all questions before proceeding.'));
        return false;
    }

    return true;
}

Can anyone shed some light on why this isn’t working as I’d expect? There has to be a more elegant way of doing this.

EDIT:

This is what I ended up with. Probably a little different than most since my form elements are dynamically populated based on an array of questions, but the general idea should apply. Normally, you could just count the number of radio elements, but in my case, rather than looping through the elements and checking the type, it was just easier to count my array of questions.

public function isValid($data)
{
    $valid_values = 0;

    parent::isValid($data);

    foreach ($this->getValues() as $value) {
        if ($value >= 1 && $value <= 10) {
            $valid_values++;
        }
    }

    if ($valid_values <> count($this->_questions)) {
        $this->setErrorMessages(array('Please answer all questions before proceeding.'));
        return false;
    }

    return true;
}

Still not sure this is the most elegant way, but it works for my particular case.

  • 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-06-03T05:58:45+00:00Added an answer on June 3, 2026 at 5:58 am

    The way I see it, if you really have only that one validator one each field, why don’t manage that in the controller, instead of overriding isValid ?

    if ($form->isValid($_POST))
    { /* success */ }
    else
    { /* error as at least one field is missing */ }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.