密码和验证是真的,尽管没有设置其中一个。

时间:2015-01-23 02:39:23

标签: php isset

我有一些检查密码的功能,并将其与验证进行比较,这是我的功能:

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);

            }
        }
    }

在我的班级$ this-> _input指$ _POST。然后我有一个类来设置这样的错误:

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;
            }
        }

    }

我期待当我提交表单并且我的密码和验证字段为空时,只显示“需要密码”或“需要验证”,而不显示错误“密码与验证不同”。但是当我是只填写我的密码字段,显示“需要验证”,第二个错误“密码与验证不同”,因为我的验证仍然是空的。我的逻辑或其他东西有问题吗?

1 个答案:

答案 0 :(得分:0)

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

此代码将返回true。因此,将验证字段设为空还是会将其与密码字段进行比较。这就是解决方案:

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