Hallo,
Ik heb op basis van http://www.phphulp.nl/php/script/classes/form-validation-class/1946/ zelf een validatie klasse gemaakt.
Ben een beginner op het gebied van OOP en ik hoop dat iemand mij goede feedback kan geven of dit juist is.


<?php
class Forms_FormsValidation {

    private $_required;
    private $_minLength;
    private $_maxLength;
    private $_alphaNumeric;
    private $_errors;

    public function __construct($validate, $posts) {
        array_pop($posts);
        foreach ($validate as $arraykey => $value) {            
            foreach ($value as $key => $values) {
                if (method_exists($this, "set$key")) {                    
                    $set = 'set'.ucfirst($key);
                    $get = 'get'.ucfirst($key);
                    $this->$set($posts["$arraykey"], $values);
                    if( $this->$get() != '') {
                        $this->_errors[$arraykey] .= $this->$get();
                    }                    
                }
            }
        }          
    }
    
    public function setValidation(){
        if ( empty($this->_errors) ){return TRUE;}return FALSE;
    }

    public function getRequired() {
        return $this->_required;
    }

    public function setRequired($value, $ruleValue) {
        if (empty($value) && $ruleValue == TRUE) {
            $this->_required = 'this field is required';
        }
    }

    public function getMinLength() {
        return $this->_minLength;
    }

    public function setMinLength($value, $ruleValue) {
        if (strlen($value) < $ruleValue) {
            $this->_minLength = 'must be longer than' . $ruleValue . '';
        }
    }

    public function getMaxLength() {
        return $this->_maxLength;
    }

    public function setMaxLength($value, $ruleValue) {
        if (strlen($value) > $ruleValue) {
            $this->_maxLength = 'must be shorter than' . $ruleValue . '';
        }
    }

    public function getAlphaNumeric() {
        return $this->_alphaNumeric;
    }

    public function setAlphaNumeric($value, $ruleValue) {
        if (!preg_match('/^([a-z0-9])+$/i', $value)) {
            $this->_alphaNumeric = 'can only contain letters and numbers';
        }
    }

}


$validateUser       = array( 'user' => array ( 'required' => TRUE, 'minLength' => 2,'maxLength' => 15, 'alphaNumeric' => TRUE ));
$validatePassword   = array( 'password' => array ( 'required' => TRUE, 'minLength' => 2,'maxLength' => 15, 'alphaNumeric' => TRUE ));
$_POST = array_map('strip_tags', $_POST);
$formsValidation = new Forms_FormsValidation($validateUser, $_POST); 
$formsValidation = new Forms_FormsValidation($validatePassword, $_POST); 
if ( $formsValidation->setValidation() === TRUE ) {echo 'TRUE';}

Reageren