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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:02:52+00:00 2026-05-29T06:02:52+00:00

I have this class class Validator implements iValidation{ protected $_fields, $_errors; public function __construct($fields){

  • 0

I have this class

class Validator implements iValidation{
    protected
        $_fields,
        $_errors;
    public function __construct($fields){
        $this->_errors = array();
        $this->_fields = $fields;
    }
    public function validate(){
        $map = unserialize( iValidation::map );
        foreach ($this->_fields as $field_type => $data){
            if ( array_key_exists( $field_type, $map ) ){
                $class = "Validate_{$map["$field_type"]}" ;
                $object = new $class($data);
                $object->validate();
                unset($object);
            }            
        }
    }
    public function getErrors(){
        return $this->_errors;
    }
}

Now this class loops through an array given in the format

$admin_test_data = array(
    "firstname" => "Alex1",
    "surname" => "Morley-Finch",
    "username" => "alex123",
    "password" => "cheese",
    "re_password" => "cheese",
    "email_address" => "alex54353hotmail.co.uk",
    "user_type" => "ADMIN",
    "add_admin" => "Add Admin",
);

I have a map that describes the type of validation on each field that is declared like so:

define(
    "VALIDATION_MAP",
    serialize(
            array(
                // FIELD => VALIDATION TYPE
                "firstname" => "Name",
                "surname" => "Name",
                "agency_name" => "Agency_Name",
                "agency_office" => "Name",
                "username" => "Username",
                "email_address" => "Email_Address",
                "password" => "Password",
            )
    )
);

interface iValidation{
    public function __construct($data);
    public function validate();
    const map = VALIDATION_MAP;
}

And I have sub classes One of which is like this:

class Validate_Name extends Validator implements iValidation{
    private $_data;
    public function __construct($data){
        $this->_data = $data;
    }
    public function validate(){
        $data = $this->_data;
        $length = strlen($data);
        if ( $length > 40 ){
            $this->_errors[] = "Cannot be more than 40 characters";
        }
        if ( $length < 3 ){
            $this->_errors[] = "Cannot be less than 3 characters";
        }
        if ( !preg_match("/^[a-zA-Z]+$/", $data) ){
            $this->_errors[] = "A-Z characters only";
        }
    }
}

And the whole framework is used like so:

$validator = new Validator($admin_test_data);
$validator->validate(); //  ^ defined earlier ^^^
$errors = $validator->getErrors();

Now after all that background info… lets get to the problem.

I’m probably doing some EXTREMELY stupid but I can’t figure it out and Google isn’t doing a good job of enlightening me ( or I’m not doing a good job of asking google )

The sub classes reference $this->_errors[] = "This is the error"; and when using die( var_dump( $this->_errors ) ); inside the sub class, the errors for that class appear as they should.

However if I call die( var_dump( $validator->getErrors() ) ); outside the classes, the array is empty.

Asif the parent has forgotten all the values the children set?

Whats going on?

  • 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-29T06:02:53+00:00Added an answer on May 29, 2026 at 6:02 am

    For each field you validate in Validator, you create an instance of the required implementation of iValidation.

    Now the child validator validates, and it finds errors. It adds those errors to its _errors instance attribute.

    When it’s done validating, the child instance is discarded. And that’s where the problem lies, since the errors found were assigned to the child instance’s _errors attribute, not that of the parent instance.

    Nothing ever gets assigned to Validator::_errors, because those properties are in different objects.

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

Sidebar

Related Questions

I have this class: public class Source extends Node { protected DistributionSampler delay ;
I have one registration page which uses custom valiadtor public class CustomValidator implements Validator
I have a simple class like this, import javax.validation.constraints.NotNull; import org.hibernate.validator.constraints.Length; public class Form
I have this class called Table: class Table { public string Name { get
I have this class. public class Foo { public Guid Id { get; set;
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have this class called SiteAsyncDownload.cs Here's the code: public class SiteAsyncDownloader { WebClient
I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =
I have this class that have a function to load other classes and create
I have a custom converter which is this : @ManagedBean @RequestScoped public class MeasureConverter

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.