So I have been reworking this
<?php
class Validation
{
public
$password,
$repeatPassword,
$username,
$email;
public static
$minPassword = 5,
$confirmPassword = 5,
$minUsername = 3,
$validEmail;
public static function validateEmail($email)
{
if(filter_var($email, FILTER_VALIDATE_EMAIL) != self::$validEmail)
{
return true;
}
else
{
return false;
}
}
public static function validatePassword($password)
{
if(strlen($password) >= self::$minPassword)
{
return (strlen($password) >= self::$minPassword);
}
else
{
return false;
}
}
public static function validateRepeatPassword($repeatPassword)
{
if(strlen($repeatPassword) >= self::$confirmPassword)
{
return (strlen($repeatPassword) >= self::$confirmPassword);
}
else
{
return false;
}
}
public static function validateUsername($username)
{
if (strlen($username) >= self::$minUsername)
{
return (strlen($username) >= self::$minUsername);
}
else
{
return false;
}
}
}
<?php
require "loadclasses.php";
$password = $username = $email = $confirmPassword = "";
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$email = trim($_POST['email']);
$confirmPassword = trim($_POST['repeatPassword']);
$errors = array();
if(Validation::validateUsername($username))
{
return true;
}
else
{
$errors[] = ["name" => "username", "error" => "invalid username"];
}
if(Validation::validateEmail($email))
{
return true;
}
else
{
$errors[] = ["name" => "email", "error" => "invalid Email"];
}
if(Validation::validatePassword($password))
{
return true;
}
else
{
$errors[] = ["name" => "password", "error" => "invalid password"];
}
if(Validation::validateRepeatPassword($confirmPassword))
{
return true;
}
else
{
$errors[] = ["name" => "repeatPassword", "error" => "Passwords must match"];
}
header('Content-Type: application/json');
if (isset($success))
{
echo json_encode($success);
}
else
{
echo json_encode(["errors" => $errors]);
}
First problem, if the username is valid the other failed validation won't show, second problem it seems it's not D.R.Y