在没有未声明的变量错误的情况下设置可变数据的样式

时间:2015-03-20 04:55:39

标签: php html validation

我有一个错误而且发生这种情况因为我正在更改$item名称,因为我希望它是大写的。我已经为我的大多数代码完成了它,但出于某种原因,我在这里遇到了一些麻烦。

以下是代码:

public function check($source, $items = array()) {
        foreach($items as $item => $rules) {
            foreach($rules as $rule => $rule_value) {

                $value = $source[$item];
                $item = escape($item);

                if ($rule === 'required' && empty($value)) {
                    $error = str_replace(array('_'), ' ', $item);
                    $this->addError(ucfirst($error) . " is required");
                } else if(!empty($value)){
                    switch($rule) {
                        case 'min':
                            if(strlen($value) < $rule_value) {
                                $this->addError(ucfirst($item) . " must be a minimum of {$rule_value} characters.");
                            }
                        break;
                        case 'max':
                            if(strlen($value) > $rule_value) {
                                $this->addError(ucfirst($item) . " must be a maximum of {$rule_value} characters.");
                            }
                        break;
                        case 'matches':
                            if($value != $source[$rule_value]) {
                                $item = str_replace(array('_'), ' ', $item);
                                $this->addError(ucfirst($item) . ' does not match ' . $rule_value);
                            }
                        break;
                        case 'unique':
                            $check = $this->_db->get('*', $rule_value, array($item, '=', $value));
                            if ($check->count()){
                                $this->addError(ucfirst($item) . ' is already in use!');
                            }
                        break;
                    }
                }
            }
        }

        if(empty($this->_errors)) {
            $this->_passed = true;
        }

        return $this;
    }

有问题的代码是:

if ($rule === 'required' && empty($value)) {
    $item = str_replace(array('_'), ' ', $item);
    $this->addError(ucfirst($item) . " is required");
}

我正在努力使其输出Password again is required而不是password_again is requiredswitch($rule)语句中的所有内容都正常工作,但就像我说的那样,我不知道为什么它在该特定行上不起作用。有人有想法吗?

确切错误:Notice: Undefined index: password confirm in C:\xampp\htdocs\classes\Validate.php on line 13

我的想法:

  1. 取消通知。这很好,但如果能解决这个问题,我真的很喜欢。
  2. 代码请求[来源&amp;项]:

    $validation = $validate->check($_POST, array(
            'email' => array(
                'required' => true,
                'min' => 7,
                'max' => 64,
                'unique' => 'users'
            ),
            'password' => array(
                'required' => true,
                'min' => 7,
                'max' => 32
            ),
            'password_confirm' => array(
                'required' => true,
                'matches' => 'password'
            )
        ));
    

    Var Dump结果:

    string(5) "email" 
    Notice: Undefined property: Validate::$addError in      C:\xampp\htdocs\classes\Validate.php on line 18
    NULL string(8) "password" 
    Notice: Undefined property: Validate::$addError in  C:\xampp\htdocs\classes\Validate.php on line 18
    NULL string(16) "password confirm" 
    Notice: Undefined property: Validate::$addError in C:\xampp\htdocs\classes\Validate.php on line 18
    NULL 
    Notice: Undefined index: password confirm in     C:\xampp\htdocs\classes\Validate.php on line 13
    

    新工作代码:

    if ($rule === 'required' && empty($value)) {
        $error = str_replace(array('_'), ' ', $item);
        $this->addError(ucfirst($error) . " is required");
    }
    

1 个答案:

答案 0 :(得分:1)

您错过了一个空检查,这就是为什么在缺少电子邮件,密码或密码确认时显示通知的原因:

            if( !empty( $source[$item] ) )
            {
                $value = $source[$item];
                $item = escape($item);

                if ($rule === 'required' && empty($value)) {
                    $item = str_replace(array('_'), ' ', $item);
                    $this->addError(ucfirst($item) . " is required");
                } else if(!empty($value)){
                    switch($rule) {
                        case 'min':
                            if(strlen($value) < $rule_value) {
                                $this->addError(ucfirst($item) . " must be a minimum of {$rule_value} characters.");
                            }
                        break;
                        case 'max':
                            if(strlen($value) > $rule_value) {
                                $this->addError(ucfirst($item) . " must be a maximum of {$rule_value} characters.");
                            }
                        break;
                        case 'matches':
                            if($value != $source[$rule_value]) {
                                $item = str_replace(array('_'), ' ', $item);
                                $this->addError(ucfirst($item) . ' does not match ' . $rule_value);
                            }
                        break;
                        case 'unique':
                            // $check = $this->_db->get('*', $rule_value, array($item, '=', $value));

                            if ( 1 == 1 ){
                                $this->addError(ucfirst($item) . ' is already in use!');
                            }
                        break;
                    }
                }
            }
            else
            {
                $this->addError( $item . ' cannot be empty!' );
                break;
            }

我建议使用更简单的解决方案。

按要求完全复制:

    function escape( $sValue )
    {
        return trim( $sValue );
    }
class test
{
    public $aErrors = array();
    public function addError( $sError )
    {
        $this->aErrors[] = $sError;
    }

    public function check($source, $items = array()) {
            foreach($items as $item => $rules) {
                foreach($rules as $rule => $rule_value) {

                    if( !empty( $source[$item] ) )
                    {
                        $value = $source[$item];
                        $item = escape($item);

                        if ($rule === 'required' && empty($value)) {
                            $item = str_replace(array('_'), ' ', $item);
                            $this->addError(ucfirst($item) . " is required");
                        } else if(!empty($value)){
                            switch($rule) {
                                case 'min':
                                    if(strlen($value) < $rule_value) {
                                        $this->addError(ucfirst($item) . " must be a minimum of {$rule_value} characters.");
                                    }
                                break;
                                case 'max':
                                    if(strlen($value) > $rule_value) {
                                        $this->addError(ucfirst($item) . " must be a maximum of {$rule_value} characters.");
                                    }
                                break;
                                case 'matches':
                                    if($value != $source[$rule_value]) {
                                        $item = str_replace(array('_'), ' ', $item);
                                        $this->addError(ucfirst($item) . ' does not match ' . $rule_value);
                                    }
                                break;
                                case 'unique':
                                    // $check = $this->_db->get('*', $rule_value, array($item, '=', $value));

                                    if ( 1 == 1 ){
                                        $this->addError(ucfirst($item) . ' is already in use!');
                                    }
                                break;
                            }
                        }
                    }
                    else
                    {
                        $this->addError( $item . ' cannot be empty!' );
                        break;
                    }
                }
            }
}
}
$_POST[ 'email' ] = '';
$_POST[ 'password' ] = '';
$_POST[ 'password_confirm' ] = '';
$aValidation = $cTest->check( $_POST, 
    array( 'email' => array( 'required' => true, 'min' => 7, 'max' => 64, 'unique' => 'users' ), 
    'password' => array( 'required' => true, 'min' => 7, 'max' => 32 ), 
    'password_confirm' => array( 'required' => true, 'matches' => 'password' ) ));
var_dump( $cTest->aErrors );
var_dump( $aValidation );