SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Validation in a class inheritance hierachy

    Hello everyone,

    If I've got a inheritance hierachy like "Field < StringField < EmailField" and I want to be able to have some validation logic in all of them with different generalization.

    Field for example can check if a field is empty (required), StringField can check max/min length, etc. Now I want to be able to run $field->validate() and it will all be checked. Is it possible?

    Thanks,

    Martin Lundberg
    Sweden

  2. #2
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You could override the method in each of the inheriting classes to run the parent's validate method, and then add its own specific code.

    PHP Code:
    class Field {
     public function 
    validate () {
      
    //Generic field validation, store result in $blnValidated

      
    return $blnValidated;
     }
    //function
    }//class

    class StringField extends Field {
     public function 
    validate () {
      
    //Validate string, store result in $blnValidated

      
    return ($blnValidated && parent::validate());
     }
    //function
    }//class

    class EmailField extends StringField {
     public function 
    validate () {
      
    //Validate email, store result in $blnValidated

      
    return ($blnValidated && parent::validate());
     }
    //function
    }//class

    $field = new EmailField();
    $blnValid $field->validate(); 
    Probably needs a bit of work though!

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Check out this post here, it addresses exactly what you're after.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Check out this post here, it addresses exactly what you're after.
    I started out by using polymorphism solution where the base class constructor ran an init method but if I've got the inheritance hierarchy I wrote, if I create a EmailField the Field constructor will be run and it will call init which will call the init function in EmailField but not in the StringField.

    The thing is that I want all the classes to add to an array in the base class called options. For example Field adds $this->options["required"] = true, StringField adds $this->options["max_length"] = some default value, etc. When a field is created you can send in an assoc array which changes the options. Then when I run validate it should all be validated.

    Right now most of the code is in the base class and the sub-classes mostly add to the options array and create their validate class.

    Hope I'm not making a mess of trying to describe what I want.

    Martin Lundberg
    Sweden

  5. #5
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you want to run the parent class's constructor, you have to explicitly call it with parent::__construct()

  6. #6
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, right now the StringField class looks like this:

    PHP Code:
    class StringField extends Field
    {
        public function 
    __construct()
        {
            
    // Declare options and their default value
            
    $this->options["max_length"] = null;
            
    $this->options["min_length"] = null;

            
    // Call parent constructor
            
    if (is_array(@func_get_arg(0)))
                
    parent::__construct(@func_get_arg(0));
            else
                
    parent::__construct();
        }

        protected function 
    validate()
        {
            
    // First let the parent class make more general validation
            
    if (!parent::validate())
                return 
    false;

            
    // If the value is longer then the maximum length
            
    if (!is_null($this->options["max_length"]) && $this->value $this->options["max_length"])
            {
                
    $this->validation_error "##validationError.ExceededMaximumLength##";
            }

            
    // If the value is shorter then the minimum length
            
    if (!is_null($this->options["min_length"]) && $this->value $this->options["min_length"])
            {
                
    $this->validation_error "##validationError.DeceededMinimumLength##";
            }

            return empty(
    $this->validation_error);
        }

    I don't like that I have to do the last part in the constructor and the first part in validate function. I want a nicer way of doing this Maybe I need to rethink the whole design.

    Martin Lundberg
    Sweden


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •