Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php classes... validation

im am making a php validation class with sub classes that extend it, eg, mobile, suburb, credit_card, ect

so, the idea is you can call

$validation = new Validation('mobile');
$valid = $validation->validate($number);

$validation->type('suburb');
$valid2 = $validation->validate($suburb);

now my idea for doing this is having

class Validation() {
    private $v_type = null;

    function __construct($type) {
        $this->type($type);
    }

    public function type($type) {
        $this->v_type = new $type();
    }

    public function validate($info) {
        return $this->v_type->validate($info);
    }
}

as a very basic example

but is there a better way of doing this?

like image 807
Hailwood Avatar asked Jul 15 '10 06:07

Hailwood


People also ask

What is the best PHP helper class for form validation?

GitHub - davidecesarano/Validation: Simple PHP helper class for Validation. This PHP class is useful to validate an HTML form fields. Do you use PSR-7? Try Embryo Validation, the new version of this repository.

What is validation in PHP?

Introduction to Validation in PHP Validation in PHP is the process where we check if the input information in the various fields in any form such as text, checkbox or radio button, etc, submitted by the end-user in the form is correct or not using HTML code. There are 2 kinds of validation available in PHP:

What is the use of form validation class?

This class is meant to validate data entered in forms. It provides 11 types of validation. To specify a field to validate it requires the field name and, in some cases, other arguments. The class will return false if the validation failed.

Is my class doing too much validation?

Your class does way too much. This is just using a class as a collection of functions. Each validation can be seen as a "responsibility" and thus your class completely violates the single responsibility principle. Imagine that in the future you want to add a new validation.


2 Answers

You could do it this way, but it could be improved. Having the actual validators capsule their own validation logic is good. Extending them from a base class isn't. Let's implement an interface instead. This way, any class can be a Validator.

interface IValidate
{
    public function validate($value);
}

Your validators would look like this then:

class IsNumeric implements IValidate
{
    public function validate($value)
    {
        return is_numeric($value);
    }
}

and

class GreaterThan implements IValidate
{
    protected $_value;
    public function __construct($value)
    {
        $this->_value = $value;
    }
    public function validate($value)
    {
        return $value > $this->_value;
    }
}

You'd still have a main Validator class. Unlike in your example, the Validator below accepts multiple Validators, which will allow you to create a Filter Chain.

class Validator implements IValidate
{
    protected $_validators;

    public function addValidator(IValidate $validator)
    {
        $this->_validators[] = $validator;
        return $this;
    }
    public function validate($value)
    {
        foreach($this->_validators as $validator) {
            if ($validator->validate($value) === FALSE) {
                return FALSE;
            }
        }
        return TRUE;
    }
}

And this could be used like:

$validator = new Validator;
$validator->addValidator(new IsNumeric)
          ->addValidator(new GreaterThan(5));

var_dump( $validator->validate('ten') ); // FALSE
var_dump( $validator->validate('10') );  // TRUE
var_dump( $validator->validate('1') );   // FALSE

The above is pretty much a Command pattern. And due to the Validator implementing IValidate as well, it is also a Composite. You could take the Validator chain from above and stack it into another Validator Chain, e.g.

$numericGreaterThanFive = new Validator;
$numericGreaterThanFive->addValidator(new IsNumeric)
                       ->addValidator(new GreaterThan(5));

$otherValidator = new Validator;
$otherValidator->addValidator(new Foo)
               ->addValidator(new Bar)
               ->addValidator($numericGreatherThanFive);

For convenience, you could add a static factory method for creating Validators with the actual Validation Command objects (as shown elsewhere).

On a sidenote: the Zend Framework already has an extensive number of Validators you can build on. Since ZF is a component library, you can use them without having to migrate your entire application to ZF.

like image 87
Gordon Avatar answered Oct 07 '22 03:10

Gordon


Usually you do these kind of things using the Factory pattern, something like this:

class ValidatorFactory {
    public static function get($type) {
        $validator = "Validator_$type";
        return new $validator();
    }
}

$valid = ValidatorFactory::get('mobile')->validate($number);

Would of course need some error checking and such, but you should get the idea

like image 2
Dennis Haarbrink Avatar answered Oct 07 '22 03:10

Dennis Haarbrink