[RESOLVED] Validation (yes again)
Results 1 to 6 of 6

Thread: [RESOLVED] Validation (yes again)

  1. #1
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    384

    resolved [RESOLVED] Validation (yes again)

    So I have been reworking this

    PHP Code:
    <?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($emailFILTER_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 Code:
    <?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
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,807
    You can do all the validations first, then decide what to return afterwards by checking the contents of the $errors array. Also, you could try something like this to make it a bit DRYer:
    PHP Code:
    $errors = array();
    $fields = array(
      
    'username' => array(
        
    'validator' => 'validateUsername',
        
    'message'   => 'invalid username'
      
    ),
      
    'email' => array(
        
    'validator' => 'validateEmail',
        
    'message'   => 'invalid email',
      );
      
    'password' => array(
        
    'validator' => 'validatePassword',
        
    'message'   => 'invalid password'
      
    ),
      
    'repeatPassword' => array(
        
    'validator' => 'validateRepeatPassword',
        
    'message'   => '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']];
        }
      }
    }
    if(empty(
    $errors)) {
      return 
    true;

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,489
    PHP Code:
    if(!Validation::validateUsername($username))
    {
        
    $errors[] = ["name" => "username""error" => "invalid username"];
    }
    ...
    etc
    If at the end of all that $errors isn't empty then you know there was (at least) one error.

    EDIT: What he said.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #4
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    384
    PHP Code:
    if(!Validation::validateUsername($username)) 

        
    $errors[] = ["name" => "username""error" => "invalid username"]; 

    ...
    etc
    I did try that didn't work it seemed.


    Thanks Nogdog, still not sure when I should use arrays
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,807
    Quote Originally Posted by cluelessPHP View Post
    ...
    Thanks Nogdog, still not sure when I should use arrays
    There's always more than one way to do it. However, when I see myself doing essentially the same thing over and over, I tend to think first of some way to put it into a loop. That often suggests to me that I want something I can iterate over multiple times. That usually leads me to an array with a foreach() loop, or in more complex situations, perhaps a class that implements Iterator interface. (Most of the time the array approach is sufficient for me.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    384
    Ah, loops I guess seem better. I'm guessing if it's in a loop you won't need to repeat yourself as much.


    Still working my way through these, they seem good tutorials

    https://www.udemy.com/learn-object-o...arn/v4/content
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •