2

Basically I'm trying to create a login, the email validation doesn't seem to pass. I've been looking around for an example but I'm genuinely not sure, doing it statically seems easy enough, however I vaguely suspect using static method would be incorrect to use as a login method(perhaps I'm over thinking it)

    <?php

    require ("Database.class.php");

    class Login
    {   
        private 
        $email,
        $password,
        $database,
        $db = null;

        public function __construct()
        {
            $this->db = new Database;
        }

        public function validEmail($email)
        {   
            return (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE);  
        }   

        public function emptyPassword($password)
        {
            return(empty($password) !== TRUE);
        }
        public function validPassword($password)
        {
            $query = $this->db->prepare("select * from username");
            return $query->fetch(PDO::FETCH_ASSOC);
        }
    }


<?php 

require "classes/Login.class.php";
require "loadclasses.php";

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $email = $pass = "";
    $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);     

    $email = $post['email-login'];
    $pass = $post['password-login'];        
    $errors = array();

    $fields = array(
     'email-login' => array(        
         'validate' => 'validEmail',
         'message'   => 'Enter a valid email address'
        ),
         'password-login' => array(         
         'validate' => 'emptyPassword',
         'message'   => 'Password required'
        )
    );

    $login = new Login();
    foreach($fields as $key => $value) 
    {
        $validation_result = $login->$value['validate']($value);

        if(!$validation_result) 
        {
            $errors[] = ['name' => $key, 'error' => $value['message']];
        }
    }

    if(empty($errors)) 
    {
         $success = ['response' => 'true']; 
         session_start();
    }   

}


header('Content-Type: application/json');
if (empty($errors))
{
    echo json_encode($success);
}
else
{
    echo json_encode(["errors" => $errors]);
}           

As mentioned I'm aware I could do something similar to this:

$errors = array();

$fields = array( 
  'username' => array( 
    'validator' => 'validateUsername', 
    'message'   => 'Username must be between three and fourteen alphanumeric characters' 
  ), 
  'email' => array( 
    'validator' => 'validateEmail', 
    'message'   => 'Please enter a valid email', 
  ), 
  'password' => array( 
    'validator' => 'validatePassword', 
    'message'   => 'Password must be a minimum of seven characters' 
  )
);

if(!Validation::validateRepeatPassword($password, $repeatPassword)) 
{ 
    $errors[] = ["name" => "repeatPassword", "error" => "Passwords must match"]; 
}
foreach($post as $key => $value) 
{
    if(isset($fields[$key])) 
    {
        if(!Validation::{$fields[$key]['validator']}($value)) 
        {
            $errors[] = ['name' => $key, 'error' => $fields[$key]['message']];
        }
    }
}

The main problem as I mentioned is I'm fairly sure that would be the wrong way to approach this problem

1
  • Btw, return(empty($password) !== TRUE); why not simply: return empty($password);? Commented Sep 14, 2017 at 19:35

1 Answer 1

2

The problem seems to be here:

$validation_result = $login->$value['validate']($value);

When you do that, you're actually passing $value which is an array (according to the foreach). You're not actually passing the email

So, according to your code, you should change your validation array to something like:

$fields = array(
  'email-login' => array(        
    'validate' => 'validEmail',
    'message' => 'Enter a valid email address',
    'value' => $email,
  ),
  'password-login' => array(         
    'validate' => 'emptyPassword',
    'message' => 'Password required',
    'value' => $pass,
  )
);

And then, change your validation line to:

$validation_result = $login->$value['validate']($value['value']);
Sign up to request clarification or add additional context in comments.

2 Comments

No problem. Also, I suggest you use better variable names. If, for example, the foreach variable was called $setup instead of $value you'd probably catch stuff like that easily
Makes sense, I always have trouble with variable names, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.