PHP验证不起作用,没有明确的错误

时间:2015-10-08 01:56:16

标签: php forms apache validation

我重新推出了一个验证类,用于检查登录/注册表单,以代替纸质上传表单。我复制了语法,并为检查论文明确地重新创建了一个新函数。我现在的问题是它不起作用,并没有明确的迹象表明原因。我正在检查PHP error.log并且它没有显示任何异常。

请使用表单和PHP代码继承我的upload.php:

<?php
    require 'core/init.php';
    $user = new User();

    $user->protect();

    $_error = false;
    $_papererror = false;

    if (Input::exists()) {
        if (Token::check(Input::get('token'))) {
            //print_r($_FILES['paper']);

            $validate = new Validate();
            $papervalidate = new Validate();
            $validation = $validate->check($_POST, array(
                'papername' => array('required' => true, 'max' => 50)
            ));
            $papervalidation = $papervalidate->checkPaper($_FILES, array(
                'paper' => array('required' => true, 'type' => Input::get('paper')['type'])
            ));

            if($validation->passed() && $papervalidation->passed()) {
                // handle upload, and then redirection
                echo "Success";
            } else {
                $_error = true;
            }
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Hutcheson Website</title>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <!--[if lt IE 9]>
        <script src="js/html5shiv.min.js"></script>
        <script src="js/respond.min.js"></script>
        <![endif]-->
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
    </head>
    <body>
        <?php include 'includes/_navbar.php'; ?>

        <?php
            if($_error === true || $_papererror == true) {
                ?>
                    <div class="container-fluid">
                        <div class="row">
                            <div class=" col-lg-4 col-lg-offset-4 col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3">
                                <div class="alert alert-danger">
                                    <ul style="list-style: none;">
                                        <?php
                                            foreach($validation->errors() as $error) {
                                                echo "<li><strong>ERROR!!</strong> " . $error . "</li>";
                                            }
                                            foreach($papervalidation->errors() as $error) {
                                                echo "<li><strong>ERROR!!</strong> " . $error . "</li>";
                                            }
                                        ?>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php
            }
        ?>

        <div class="container-fluid">
            <div class="col-lg-6 col-lg-offset-3 col-md-8 col-md-offset-2">
                <form action="" method="post" class="form-horizontal validate" enctype="multipart/form-data">
                    <fieldset>
                        <legend>Upload a Paper</legend>
                        <div class="form-group">
                            <label for="papername" class="col-md-2 control-label">Paper Name</label>
                            <div class="col-md-10">
                                <input type="text" class="form-control" id="papername" name="papername" placeholder="Give me a fancy name" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="format" class="col-md-2 control-label">Formatting</label>
                            <div class="col-md-10">
                                <select name="format" id="format" class="form-control">
                                    <option value="0" selected>MLA</option>
                                    <option value="1">APA</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="paper" class="col-md-2 control-label">Paper</label>
                            <div class="col-md-10">
                                <input type="file" class="form-control" id="paper" name="paper" />
                                <span class="help-block">File formats accepted are .doc .docx .txt .rtf</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-xs-10 col-xs-offset-2">
                                <button type="submit" class="btn btn-primary col-xs-12">Submit</button>
                            </div>
                        </div>
                        <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
                    </fieldset>
                </form>
            </div>
        </div>
    </body>
</html>

这是Validate.php类,我调用它来验证表单:

<?php
/**
 * Created by PhpStorm.
 * User: jonathanstowe
 * Date: 12/12/13
 * Time: 1:43 AM
 */

class Validate {
    private $_passed = false;
    private $_errors = array();
    private $_db = null;

    private $_fields = array(
        'username' => 'Username',
        'password' => 'Password',
        're_password' => 'Repeat Password',
        'name' => 'Name',
        'cur_pass' => 'Current Password',
        'new_pass' => 'New Password',
        're_pass' => 'Repeat Password',
        'papername' => 'Paper name',
        'paper' => 'A paper',
        'format' => 'Format is required'
    );

    public function __construct() {
        $this->_db = DB::getInstance();
    }

    /*
     *
     */
    public function check($source, $items = array()) {
        foreach($items as $item => $rules) {
            foreach($rules as $rule => $rule_value) {
                $value = trim($source[$item]);
                $item = escape($item);

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

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

        return $this;
    }

    public function checkPaper($source, $items = array()) {
        foreach($items as $item => $rules) {
            foreach($rules as $rule => $rule_value) {
                $value = $source['paper'];

                $item = escape($item);

                //die($source['paper']);

                if($rule === 'required' && empty($value['paper'])) {
                    $this->addError("{$this->refactor($item)} is required");
                } else if(!empty($value)) {
                    switch($rule) {
                        case 'type':
                            $_types = array(
                                'application/pdf',
                                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                                'text/plain',
                                'text/rtf',
                                'application/msword'
                            );
                            if(!in_array($value, $_types)) {
                                $this->addError("{$this->refactor($item)} must be in a .doc .docx .txt .rtf .pdf file format");
                            }
                            break;
                    }
                }
            }
        }

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

        return $this;
    }

    /*
     *
     */
    private function addError($error) {
        $this->_errors[] = $error;
    }

    /*
     *
     */
    public function errors() {
        return $this->_errors;
    }

    /*
     *
     */
    public function passed() {
        return $this->_passed;
    }

    /*
     *
     */
    public function refactor($item) {
        $field = array('username', 'password', 're_password', 'name', 'cur_pass', 'new_pass', 'rep_pass');

        $item = strtolower($item);

        if(in_array($item, $field) === true) {
            return $this->_fields[$item];
        }
        return $this->_fields[$item];
    }
}

在我的mac上使用Console.app查看apache2 error.log在尝试上传任意一对格式时没有显示任何错误。

我已经尝试将$ _FILES路径更改为更少动态,更静态,因为这是唯一需要验证的上传表单。

1 个答案:

答案 0 :(得分:0)

我弄清楚我哪里出错了。由于在提交表单时创建了全局$_FILES变量,因此它将显示为非空,并设置。因此,要检查我必须将错误编号与$_FILES错误4进行比较。

所以要解决这个问题,这里有正确的checkPaper()函数:

public function checkPaper($source, $items = array()) {
        foreach($items as $item => $rules) {
            foreach($rules as $rule => $rule_value) {
                $value = $source['paper'];
                $item = escape($item);

                //die($source['paper']);

                if($rule === 'required' && $value['error'] == 4) {
                    $this->addError("{$this->refactor($item)} is required");
                } else if(!empty($value)) {
                    switch($rule) {
                        case 'type':
                            $_types = array(
                                'application/pdf',
                                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                                'text/plain',
                                'text/rtf',
                                'application/msword'
                            );
                            if(!in_array($rule_value, $_types)) {
                                $this->addError("{$this->refactor($item)} must be in a .doc .docx .txt .rtf .pdf file format");
                            }
                            break;
                    }
                }
            }
        }

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

        return $this;
    }