Hi, I have a validation class which I would like to use to check all values in my application are within allowed constraints.
I am passing an object to a static function within the validation class, from another class (in this case User)
function validate() {
$errors = Validation::validate($this);
}
In the validation class, I create a new object and then proceed through the properties of the passed parameter object (or at least that is what I would like to do).
function validate($object) {
$validation = new Validation();
print_r($object);
print_r('<br />');
foreach($object as $key => $val) {
print_r($val);
isset($val->maxlength) ? $validation->validateLengthNoMoreThan($val->value, $val->maxlength) : null;
isset($val->minlength) ? $validation->validateLengthAtLeast($val->value, $val->minlength) : null;
isset($val->required) && ($val->required == true) ? $validation->validateNotBlank($val->value) : null;
}
return $validation->errors;
}
I am printing out values within the function purely for test purposes. What I don't understand is why the object prints out fine outside of the foreach loop, but if I try to access the values within the loop, nothing is displayed.
This is what is displayed OUTSIDE of the foreach loop:
User Object ( [username:protected] => Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 ) [firstname:protected] =Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 ) [lastname:protected] =Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 ) )
The validation class does NOT extend the User class, so I understand why the values would not be available, just not why they are available outside of the loop but not inside of it.
Also, what would be the best way to carry out validation on protected/private properties?
Any advice/tips would be greatly appreciated.
Thanks.