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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:02:28+00:00 2026-06-04T21:02:28+00:00

I have looked for better ways to handle validation for as long as I’ve

  • 0

I have looked for better ways to handle validation for as long as I’ve been developing web applications. Catching multiple validation errors is frequently necessary, so I wanted to know if there was a better way to do it than the following.

Right now I have an assert method in a framework I’ve developed myself. An example of the method is this:

assert(($foo == 1), 'Foo is not equal to 1');

If the condition in the first argument is false, the error message in the second argument is added to an $errors array (which is wrapped in a class (referenced by $eh below) that provides convenience functions such as hasErrors()).

This method works but is messy in practice. Consider this code:

public function submit($foo, $bar, $baz)
{
    assert(($foo == 1), 'Foo is not equal to 1');
    assert(($bar == 2), 'Bar is not equal to 2');

    if (!$eh->hasErrors())
    {
        assert(($baz == 3), 'Baz is not equal to 3');

        if (!$eh->hasErrors())
        {
            finallyDoSomething();
            return;
        }
    }

    outputErrors();
}

This is something fairly common. I want to check two conditions before moving on, and then if those pass, check a third condition before finally doing what I want to do. As you can see, most of the lines in this code are related to validation. In a real application, there will be more validation and possibly more nested if statements.

Does anyone have a better structure for handling validation than this? If there are frameworks that handle this more elegantly, what are they and how do they accomplish it? Multiple nested if statements seem like such a ‘brute-force’ solution to the problem.

Just a note, I understand it would probably be a good idea to wrap some common validation functions in a class so that I can check length, string format, etc., by calling those functions. What I am asking is a cleaner approach to the code structure, not how I am actually checking the errors.

Thank you!

  • 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-04T21:02:29+00:00Added an answer on June 4, 2026 at 9:02 pm

    Please check Respect\Validation. It’s a library built for that purpouse. It can handle multiple rules very easily and uses exceptions for errors. Here is a quick sample:

    <?php
    
    use Respect\Validation\Validator as v;
    
    $usernameValidator = v::alnum()->noWhitespace()->length(1,15);
    
    $valid = $usernameValidator->validate("alganet"); //$valid now == true
    $valid = $usernameValidator->validate("ácido acético"); //$valid now == false
    

    Now using exceptions:

    try {
        $usernameValidator->assert("foo # bar");
    } catch (Exception $e) {
        $errors = $e->findMessages('alnum', 'noWhitespace', 'length');
    }
    

    In the sample above, the $errors variable would be something like this:

    array(
        "alnum" => '"foo # bar" must contain only letters and digits',
        "noWhitespace" => '"foo # bar" must not contain whitespace',
        "length" => null
    )
    

    I broke two previously declared rules using “foo # bar”: It has whitespace and it has a non-alnum char. For every rule that doesn’t pass, a message will be returned. Since “length” is OK, the error message is null.

    The documentation includes several more samples including nested, hierarchical rules and better exception handling. Also has a extensive list of samples for all of the 30+ built in validators.

    Hope that helps!

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

Sidebar

Related Questions

Ok. I have looked EVERYWHERE and my little brain just can't understand a better
I have looked on the web and the discussions/examples appear to be for traditional
I've looked all over the web only to find ways to make searches insensitive
I have been making applications in objective-c for a while now and feel i
I have been looking thoroughly through the Web and I cannot seem to find
Quick question (and sorry if it's already been asked, I have looked but couldn't
Have looked quite hard for this answer but having no luck. I have 3
I have looked and tried but don't see where I can stop some being
I have looked at a few of the well-known AOP-oriented frameworks for .Net such
I have looked at the usual suspects...Spark, NHaml, etc. They all seem to be

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.