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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:28:54+00:00 2026-05-23T10:28:54+00:00

I want to make a class in OOP PHP to validate forms. However, I’ve

  • 0

I want to make a class in OOP PHP to validate forms. However, I’ve having trouble structuring this.
Initially I thought of creating individual functions for each type of validation (check length of submitted data, check whether it’s a number or not, etc), then another function to check whether data passed the validation tests and pass errors into an array.
I’m getting stuck though as my code is becoming very long and difficult to manage- I’m pretty new, so how would you approach this problem?

  • 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-23T10:28:54+00:00Added an answer on May 23, 2026 at 10:28 am

    As i was reading through your post, a question came into my mind about what you write:

    Why, instead of validating a form, dont you validte your model’s objects?
    I mean, in an OOP way of looking things your model´s object (or domain objects) are the ones who knows what data is valid or not for each of their attributes.
    Not doint that, and pushing that logic into the UI makes your design fragile, UI dependant and harder to maintain. If you add a new attribute to one of your model’s object, you’ll have to modify the form validator as well.

    If you go with Objects Validation, the idea is that an object cannot be created in an invalid state. If you try to modify it with invalid data, an exception will be thrown.
    This makes easy to work with forms. The only think you have to do is populate your objects and watch for exceptions thrown in that process.
    This is only an idea to get you started and see another way of solving this problem.

    Regarding your question about Forms Validation, as the other guys said, it is always better not to reinvent the wheel and go for an existing, proven, validation framework.

    However, if you are curious about it, here is one of the many ways you can do it:

    Let’s go through the things you need: you are talking about a form that needs to be validated with one or more validation functions. Then you talk about a function that tells you whether the form passed the validation or not, and as a result you got the list of errors found during the validation phase.

    As you talk about OOP, the way to go is to give each concept or idea of your problem domain (the domain of form validation) entity via a class that represents it that model the behavior they have.

    So, it is natural to think about a FormValidator class with a list of ValidationRule instances, where each one colaborates in the validation process. This validation process is done by calling the validate function of the FormValidator. Also, each ValidationRule will give, as result of calling it´s own validate method an instance of the ValidationRuleResult class, that tells whether the validation was successful or not, along with an error message and additional data (if needed) about the validation. Once all the validation rules were evaluated, the validate method of the FormValidator class will return an instance of ValidationResult class, that summarizes all the validation results of the rules evaluated providing the list of errors found.

    To get this down to earth, here is the sample model we’re talking about:

    A sample implementation

    Disclaimer: please bear in mind that, as any design, it may contains flaws. The following is intended to help you to solve your problem, not to be a complete solution.

    class FormValidator {
        private $_validationRules;
    
        function __construct() {
                $this->_validationRules = array();
        }
    
        // Registers a new validation rule
        function addRule($aValidationRule) { $this->validationRules[] = $aValidationRule; }
    
        // Validates $aForm, evaluating each of the $_validationRules defined
        function validate($aForm) {
                $aValidationResult = new ValidationResult();
    
                foreach($this->_validationRules as $aValidationRule) {
                        $aValidationRuleResult = $aValidationRule->validate($aForm);
                        $aValidationResult->addResult($aValidationRuleResult);
                }
    
                return $aValidationResult;
        }
    }
    
    abstract class ValidationRule {
        private $_fieldName;
    
        // The form's field name to be validated
        function __construct($aFieldName) {
                $this->_fieldName = $aFieldName;
        }
    
        function fieldName() { return $this->_fieldName; }
    
        // Returns an instance of ValidationResult describing the result of evaluating the ValidationRule in $aForm.
        abstract public function validate($aForm);
    }
    
    class ValidationResult {
        private $_validationRuleResults;
    
        function __construct() {
                $this->_validationRuleResults = array();
        }
    
        // Registers a validation rule result
        function addResult($aValidationRuleResult) {
                $this->_validationRuleResults[] = $aValidationRuleResult;
        }
    
        // Returns the list of the error messages of the validation rule results that did't passed
        function errorsFound() {
                $errors = array();
    
                foreach($this->validationRuleResults as $aValidationResult) {
                        if ($aValidationResult->passed()) continue;
                        $errors[] = $aValidationResult->errorMessage();
                }
    
                return $errors;
        }
    
        // Tells whether all the validation rule results passed or not
        function validationPassed() {
                foreach($this->validationRuleResults as $validationResult) {
                        if ($validationResult->passed() == false) return false;
                }
    
                return true;
        }
    }
    
    class ValidationRuleResult {
        private $_passed, $_error_message;
    
        function __construct($passed) {
                $this->_passed = $passed;
                $this->_error_message = '';
        }
    
        // Tells whether the form passed this validation rule or not
        public function passed() { return $this->_passed; }
        public function
    
        // The error message should be empty if passed to avoid confusion
        public function errorMessage { return $this->passed() ? '' : $this->_error_message; }
        public function setErrorMessage($anErrorMessage) { $this->_error_message = $anErrorMessage; }
    }
    

    You can create a validation rule this way:

    class NotEmptyValidationRule extends ValidationRule {
        public function validate($aForm) {
                $fieldName = $this->fieldName();
                $fieldValue = $aForm[$fieldName];
    
                $passed = !empty($fieldValue);
                $result = new ValidationRuleResult($passed);
    
                if (!$passed) {
                        $result->setErrorMessage("$fieldName cannot be empty");
                }
    
                return $result;
        }
    }
    

    Some things to note:

    • Im assuming that $aForm is an associative array of field name / value
    • You can note that if a validation rule passes, the result is not used (as the ValidationResult class works only on those results that didn’t pass). Remember that this is a sample only for the purpose of helping you, is not a complete solution.

    Usage

     $rule = new NotEmptyValidationRule('name');
     $validator = new FormValidator();
     $validator->addRule($rule);
    
     $aForm = $__POST['myForm'];
     $validationResult = $validator->validate($aForm);
    
    if ($validationResult->validationPassed()) {
        $errorsFound = $validationResult->errorsFound();
        // do something with the $errorMessage
        $errorMessage = array_join('<br/>', $errorsFound);        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make this class dynamically perform functions on itself by passing different
I'm not sure how to call this. Basically I want to make a class
I want to make variables and methods in php class. And want to use
I want to make a generic class that accepts only serializable classes, can it
I have style sheet with a class name changebackgroundcolor i want make change in
In Python, I want to make selected instance attributes of a class be readonly
I want to use a category to make a method on the original class
I am learning Java and I want to make my class into an observable
I want to take this merge_sort I wrote and put it into a class.
I want to make a class usable in SortedSet | SortedMap . class MyClass

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.