我正在開發一個使用php進行驗證的簡單註冊表單。問題不在於屏幕上的驗證迴應,一旦我點擊提交按鈕,沒有任何事情發生,沒有錯誤或剎車它不會顯示我錯誤是什麼,我相信我的邏輯是正確的,但我相信可能有錯誤。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;
}
}
UPDATE
糾正了@PeteR正確指出的排字錯誤,但仍然存在回顯驗證未打印出來的問題。
鏈接,形成
http://stuweb.cms.gre.ac.uk/~ob219/membership/register.php
//打開錯誤報告以查看發生了什麼ini_set('display_errors',1); error_reporting(E_ALL); – Mubo
@Mubo好感謝偉大的想法,金正日給它一個去 – Beep