0

In this simple class i want for example var_dump the rule. But this doesnt work. Does anyone have an idea?

Ok, i have made some changes. It gives back after a post true or false. It works. Is this code the good way of OOP?

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 = 'newwwwwwwwww 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';}
Bas
  • 2,330
  • 4
  • 29
  • 68

4 Answers4

1

Use var_dump in this function

public function getValidationRules()
{
    var_dump($this->_validationRules);
} 
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

var_dump($this->_validationRules); is not working because you are trying to call it in construct of class. This construct only gets called when your create instance or object of the class. That time $this->_validationRules does not contain any value you need to var_dump in

setValidationRules()

function.

That means when you do this

$formsValidation = new Forms_FormsValidation(); 

the constructor function is called and that time it has no value in it.

Try this instead for getting dump results:

public function setValidationRules( $validationRules )
{
    $this->_validationRules = $validationRules;
    var_dump($this->_validationRules);
}  
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

Calling var_dump($this->_validationRules); inside constructor not showing anything as $this->_validationRules is null at that point. you can call it after setValidationRules function call , it will work

Ganesh Bora
  • 1,133
  • 9
  • 17
0

Var_dump() is working fine in the constructor, the problem is u need to initialize the member before creating the object.

Example:

In the constructor:

public function __construct() {
     $this->_validationRules = array( 'required' => TRUE, 'minLength' => 2,'maxLength' => 15, 'alphaNumeric' => TRUE );
     var_dump($this->_validationRules);
}

Or still u can define the variable in the class member definitions as follows

class Forms_FormsValidation {        
    private $_validationRules = array( 'required' => TRUE, 'minLength' => 2,'maxLength' => 15, 'alphaNumeric' => TRUE );
Bala Varadarajan
  • 514
  • 1
  • 5
  • 17
  • Thanks, but i want to set the array in an other page and pass true the array inside the class and work with it. For example i have user and password fields, then they have both different validate rules – Bas May 09 '13 at 05:38