Home > Blockchain >  How to make a 2nd check after first row of validations
How to make a 2nd check after first row of validations

Time:10-03

I have am trying to re-create a form register validation that I seen a few weeks ago but unable to figure it out.

I want to perform one last check after the first 3 checks then display the message

validation code

public function validateSignup(): bool
{
    $this->errors = [];

    if (empty($this->name) || (strlen($this->name) < 4))  {
        $this->errors['name'] = "Username must be at least 4 characters.";
    }
    if (empty($this->email) || (filter_var($this->email, FILTER_VALIDATE_EMAIL) === false)) {
        $this->errors['email'] = "Email address is required.";
    }
    if (empty($this->password) || strlen($this->password) < 6) {
        $this->errors['password'] = "Password is required.";
    }

    return empty($this->errors);
}

This works great for the validation requirements but I want to add another step, to check if email or username is taken, I know how to do this traditionally but wanted to make it different without giving information away.

I have a Helper to tell me if an email is in the database called alreadyExists

what I am trying to accomplish is a 2nd check after that

Example

public function validateSignup(): bool
{
    $this->errors = [];

    if (empty($this->name) || (strlen($this->name) < 4))  {
        $this->errors['name'] = "Username must be at least 4 characters.";
    }
    if (empty($this->email) || (filter_var($this->email, FILTER_VALIDATE_EMAIL) === false)) {
        $this->errors['email'] = "Email address is required.";
    }
    if (empty($this->password) || strlen($this->password) < 6) {
        $this->errors['password'] = "Password is required.";
    }

    return empty($this->errors);


    ## after it checks validation with no errors check if already exists 


    if ($this->name) Helpers::alreadyExists("user", "name", $this->name) {
        $this->errors['name'] = "Unable to register user with provided data.";
    }

    return $this->errors;

}

CodePudding user response:

public function validateSignup(): bool { 
$this->errors = [];

if (empty($this->name) || (strlen($this->name) < 4))  {
    $this->errors['name'] = "Username must be at least 4 characters.";
}
if (empty($this->email) || (filter_var($this->email, FILTER_VALIDATE_EMAIL) === false)) {
    $this->errors['email'] = "Email address is required.";
}
if (empty($this->password) || strlen($this->password) < 6) {
    $this->errors['password'] = "Password is required.";
}

If(count($this->errors) > 0) {
return empty($this->errors);
}

## after it checks validation with no errors check if already exists 


if ($this->name) Helpers::alreadyExists("user", "name", $this->name) {
    $this->errors['name'] = "Unable to register user with provided data.";
}

return empty($this->errors);
}
  •  Tags:  
  • php
  • Related