【问题标题】:php validation not workingphp验证不起作用
【发布时间】:2014-01-27 00:08:55
【问题描述】:

我正在开发一个使用 php 验证的简单注册表单。问题不是验证在我点击提交按钮后在屏幕上回显,没有任何反应,没有错误或刹车它不会告诉我错误是什么,我相信我的逻辑是正确的,但我相信可能有错误。

会为我的问题提供任何建议或识别

register.php

<?php
require_once 'core/init.php';

if(Input::exists()) {
    $validate = new Validate();
    $validation = $validate->check($_POST, array(
        'username' => array(
            'required' => true,
            'min' => 2,
            'max' => 20,
            'unique' => 'users'
        ),
        'password' => array(
            'required' => true,
            'min' => 6
        ),  
        'password_again' => array(
            'required' => true,
            'matches' => 'password'
        ),  
        'name' => array(
            'required' => true,
            'min' => 2,
            'max' => 50
        ),
    ));

    if($validation->passed()) {
        echo 'Passed';
    }   else {
            print_r($validation->errors());
    }

}   
?>

<form action="" methord="post">
    <div class="field">
        <lable for="username">Username</lable>
        <input type="text" name="username" id="username" value="<?php echo escape(Input::get('username')); ?>" autocomplete="off">
    </div>

    <div class="field">
        <lable for="password">Choose Your Password</lable>
        <input type="password" name="password" id="password">
    </div>

    <div class="field">
        <lable for="password_again">Verify Password</lable>
        <input type="password" name="password_again" id="password_again">
    </div>

    <div class="field">
        <lable for="name">Your Name</lable>
        <input type="text" name="name" value="<?php echo escape (Input::get('name')); ?>" id="name">
    </div>

    <input type="submit" value="Register">
</form>

Validation.php

   <?php
class Validate {
    private $_passed = false,
            $_errors = array(),
            $_db = null;

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

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

                $value = $source[$item];

                if($rule == 'required' && empty($value)) {
                    $this->addError("{$item} is required")
                } else {

                }

            }
        }

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

        return $this;
    }

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

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

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

更新

已更正@PeteR 正确指出的错字,但仍然存在回显验证未打印出来的问题。

表格链接

http://stuweb.cms.gre.ac.uk/~ob219/membership/register.php

【问题讨论】:

  • //打开你的错误报告看看会发生什么 ini_set('display_errors', 1); error_reporting(E_ALL);
  • @Mubo 好的,谢谢,我试试看

标签: php mysql validation error-handling


【解决方案1】:

伙计,你好像有错字。

if($rule == 'requierd' && empty($value)) {

这里也少了一个分号:

$this->addError("{$item} is required");
                                  ----^

还有一个错字:

<form action="" methord="post">

应该是方法...

最后,试试这个:

private function addError($error) {

【讨论】:

  • i 在 e 之前,除了在 c 之后 ...欢呼@PeteR 总是简单的事情。
  • 要么这样,要么你就像我一样 - 打字太糟糕了! :)
  • 拼写错误已更正。不幸的是,打印通过或告诉用户遗漏了哪些字段的回显仍未显示。
  • 再次感谢,但仍然无法正常工作,所以我将滚动代码尝试查找错误
  • 您还应该接受 Dan 发布的内容,尤其是有关启用错误报告的内容。这些错误中的大多数都会引发警告并为您提供有关查看位置的指针...通常,始终在开发环境中启用错误报告,一旦代码良好,则应在生产服务器上再次禁用它.
【解决方案2】:
Input::exists()

返回 false/null 或包含错误,因此 if 语句不是 true

if(Input::exists()) {

其中的代码(验证)永远不会被执行。

调试

print_r(Input::exists());

您还应该将 if 更改为真实条件:

if(Input::exists() === true) {

在 PHP 中启用错误报告(来自 http://blog.flowl.info/2013/enable-display-php-errors/

<?php
// Put these lines to the top of your script
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
ini_set('xmlrpc_errors', true);

更新 - 又一个:

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

方法没有参数..

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

【讨论】:

  • 好建议,谢谢你之前没有使用过这种形式的调试有很大帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
相关资源
最近更新 更多