我的PHP验证有一些问题 [英] having some problem with my php validation

查看:82
本文介绍了我的PHP验证有一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用php验证的简单注册表单。一旦我点击提交按钮没有任何反应,没有错误制动剂量不是<问题,而不是屏幕上的验证回显/ span>告诉我错误是什么,我相信我的逻辑是正确的,但我相信可能有一个错误。 

会通知任何建议我的问题的识别





register.php

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

if(输入::存在()){
$ validate = new Validate();
$ validation = $ validate-> check($ _ POST,array(
' 用户名' =>数组(
' required' => true,
' min' => ; 2,
' max' => 20,
' unique' => ' users'
),
' password' =>数组(
' required' => true,
' min' => 6
),
' password_again' =>数组(
' required' => true,
' 匹配' => ' 密码'
),
' name' =>数组(
' required' => true,
' min' => 2,
' max' => 50
),
));

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

}
?>

< 表单 action = methord = post >
< div class = 字段 >
< = 用户名 > 用户名< / lable >
< < span class =code-leadattribute> input
type = text name = 用户名 id = 用户名 value = <?php echo escape(Input :: get('username')); ?> autocomplete = off >
< ; / div >

< div class = field >
< lable = 密码 < span class =code-keyword>>
选择您的密码< / l能够 >
< 输入 类型 = 密码 名称 = 密码 id = 密码 > ;
< / div >

< div class = field >
< lable = password_again > 验证密码< ; / lable >
< 输入 type = 密码 name = password_again id = password_again >
< / div >

< div class = 字段 >
< lable for = name > 您的姓名< / lable >
< input type = text name = 名称 value = <?php echo escape(输入:: get('name')); ?> id = 名称 >
< ; / div >

< input 类型 = 提交 value = 注册 >
< / form >





validate.php

 <?php  
class 验证{
private $ _传递 = false,
$ _ errors = array(),
$ _ db = null ;

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

公共功能检查($ 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}是必需的
} else {

}

}
}

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

return $ this ;
}

私函数addError(){
$ this-> _errors [] = $ error ;
}

公共函数错误(){
return $ this-> _errors;
}

公共职能通过(){
return $ this-> _passed;
}
}

解决方案

验证 = 验证();


validation =


validate-> check(

I am developing a simple registration form that validates with php. problem is rather than the validation echoing on screen once I click the submit button nothing happens, no error or brake it dose not show me what the error is, I believe my logic is correct but I believe there may be a mistake.

would appriciate any advise or identification of my problem



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>



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

解决方案

validate = new Validate();


validation =


validate->check(


这篇关于我的PHP验证有一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆