Search code examples
phpisset

Password and verification is true altough one of them is not set.


i have some function for checking password is required and compare it with verification here's my function:

public function required($field= array())
    {
        foreach($field as $value) {
            if (isset($this->_input[$value])) {
                if (empty(Security::clean($this->_input[$value]))) {
                    $messages = "is Required.";
                    error::inputError($value, $messages);
                }
            } else{
                $messages = "Not Found.";
                error::inputError($field, $messages);

            }
        }
    }

    public function password($field, $confirmasion){
        if (isset($this->_input[$field] , $this->_input[$confirmasion])){
            if ($this->_input[$field] != $this->_input[$confirmasion])
            {
                $messages = "is different with $confirmasion.";
                error::inputError($field, $messages);
                error::inputError($confirmasion, $messages);

            }
        }
    }

In my class $this->_input refers to $_POST. and then i have a class to set an error like this:

public static function inputError($field, $messages)
    {
        if (is_array($field)) {
            foreach ($field as $key){
                $newName = General::changeName($key);
                $messagesError = "$newName $messages";
                if (isset(self::$_errors[$key])){
                    return;
                }else{
                    self::$_errors[$key] = $messagesError;
                }
            }
        }else{
            $newName = General::changeName($field);
            $messagesError = "$newName $messages";
            if (isset(self::$_errors[$field])){
                return;
            }else{
                self::$_errors[$field] = $messagesError;
            }
        }

    }

i'm expecting when when i submit form and my password and verification fields is empty it display "password is required" or "verification is required" only without showing error "password is different from verification”. but when i'm only fill my password fields it showing “verification is required“ and the second error "password is different different from verification” because my verification is still empty. Is it something wrong with my logic or something?


Solution

  • isset($this->_input['fieldname'])
    

    this code will return true. Hence, having the verification field empty will still compare it to the password field.here's the solution:

    (!empty($this->_input[$field]) && !empty($this->_input[$confirmasion]))