File validator

+1 shailesh kumar · October 2, 2015
in validate class i want to increase validate properties for file like that is jpeg something but there is an array $_FILES['name'] from that i can take name of file but there is an error in validate class 

Post a Reply

Replies

Oldest  Newest  Rating
+1 Sid wadhwa · October 3, 2015
code and error ?
0 shailesh kumar · November 30, 2015
<?php
class Validate{
private $_passed = false,
$_errors = array(),
$_db = null;

public function __construct(){
$this->_db = DB::getInstance();
}

public function check($source, $items = array()){
foreach ($items as $item => $rules) {
foreach ($rules as $rule => $rule_value) {
$value = $source[$item];
if($rule === 'required' && empty($value)){
$this->addError("{$item} is required" );
} else if(!empty($value)){
switch($rule){
case 'min':
if(strlen($value) < $rule_value){
$this->addError ("{$item} must be minimum of {$rule_value} character.");
}
break;
case 'max':
if(strlen($value) > $rule_value){
$this->addError ("{$item} must be maximum of {$rule_value} character.");
}
break;
case 'digits':
if(strlen($value) != $rule_value){
$this->addError ("{$item} must be of {$rule_value} digits.");
}
break;
case 'matches':
if($value != $source[$rule_value]){
$this->addError ("{$rule_value} must be match with {$item}.");
}

break;
case 'unique':
$check = $this->_db->get($rule_value, array($item, '=', $value));
if($check->count()){
$this->addError("{$item} already exists.");

}
break;
case 'integer':
if($value !== is_int($value) ){
$this->addError ("{$item} must be integer type.");
}
break;
case 'email':
if($item != filter_var($item, FILTER_VALIDATE_EMAIL)){
$this->addError ("{$item} must be email type.");
}
break;
}

}
}
}
if(empty($this->_errors)){
$this->_passed = true;
}
return $this;
}

private function addError($error){
$this->_errors[] = $error;
}

public function errors(){
return $this->_errors;
}

public function passed(){
return $this->_passed;
}
}
  • 1

PHP

107,055 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators