I am trying to create a validation class that can be used to validate many forms of my webpage in general. It makes use of the database as well to verify records.
The problem with it is that it looks bad and somewhat inefficient. I couldn't find any alternatives and I have used too many if
-else if
statements. And also dob
makes use of an arbitrary variable. Overall the code seems bad, I know. But I couldn't find an alternative. Any help is extremely appreciated. I would also be happy if you could comment on the design and efficiency. I am willing to rewrite the whole code needed. This works, but it's all messed up. Thanks!
Class Validate {
//Take the user input into the data put it to a class.
protected $data; //Function argument
private $err; // boolean
protected $empty;
private $error;
private $clean;
private $query;
public function __construct() {
$this->query= new DBhandler();
}
private function basicSanitize($data) { //Basic level input santization to be used by other functions only.
$data=htmlspecialchars($data, ENT_QUOTES);
$data=trim($data);
return $data;
}
public function sanitize($data) { // Sanitization at a massive level to sanitize a lot of inputs at one go.
foreach ($data as $k =>$v) {
$data[$k]= $this->basicSanitize($v);
}
return $this->clean=$data;
}
public function showError($type,$data) {
return $this->error[$type.$data];
}
private function basicEmptyCheck($data) {
if($data==""|| empty($data) || !isset($data) || $data==-1) {
return true;
}
}
public function isEmpty($data) {
foreach ($data as $k =>$v) {
if($this->basicEmptyCheck($v)){
$this->error['empty']="There is an empty field";
return true;
}
}
}
private function checkLength($data,$min,$max) {
if(strlen($data)>=$min && strlen($data)<=$max) {
return true;
}
}
private function lengthErr($data,$min,$max) {
if(strlen($data)>$max) {
return "too long";
}
else if (strlen($data)<$min) {
return "too short";
}
}
public function name($data) {
if(!$this->checkLength($data,3,100)) {
$this->error['name'.$data]="Name ".$this->lengthErr($data,3,100);
}
else if(!preg_match("/^[a-zA-Z ]+$/",$data)) {
$this->error['name'.$data] = "Please don't include special characters in names";
}
else return true;
}
private function isUniqueEmail($data) {
$sql= "SELECT stuff1 FROM tablename WHERE stuff2=?";
if(!$this->query->checkIfExists($sql, "s", 'jells')) {
return true; //the value set here "jells" is for testing purposes only. This will be replaced by $data
}
}
public function email($data) {
if (!$this->checkLength($data,8,254)) {
$this->error['email'.$data]= "Enter a valid email";
}
else if (!filter_var($data, FILTER_VALIDATE_EMAIL)) {
$this->error['email'.$data] = "Enter a valid email";
}
else if (!$this->isUniqueEmail($data)) {
$this->error['email'.$data]= "This email already exists";
}
else return true;
}
public function password($data) {
if (!$this->checkLength($data,8,128)) {
$this->error['pass'.$data]='Password '.$this->lengthErr($data, 8, 128);
}
else if (count(array_keys($this->clean,$data))>1) {
$this->error['pass'.$data]='Password is too obvious';
}
else return true;
}
private function ageCheck($month,$day,$year,$limit) {
# object oriented
$date=$year."-".$month."-".$day;
$from = new DateTime($date);
$to = new DateTime('today');
if($some=$from->diff($to)->y>=$limit) {
return 1;
}
}
public function age($data,$month, $day, $year,$limit) {
if(!is_numeric($day) || !is_numeric($month) || !is_numeric($year)) {
$this->error['age'.$data] = "Please enter a valid date";
}
else if(!checkdate($month, $day, $year)){
$this->error['age'.$data] = "Please enter a valid date";
}
else if(!$this->ageCheck($month,$day, $year,$limit)) {
$this->error['age'.$data] = "You must have a minimum of ".$limit." of age to sign up";
}
else return true;
}
public function sex($data) {
if($data=='M' || $data=='F') {
return true;
}
else {
$this->error['sex'.$data]='Add a valid sex';
}
}
public function errorExists() {
if(isset($this->error)) {
return true;
}
}
}