[英]form validation using oop php
I want to validate a form using php by object oriented technique.我想通过面向对象的技术使用 php 验证表单。 I created a class validator by watching a tutorial but I want to add more rules in this class such as regular expressions and a number check and email check.
我通过观看教程创建了一个类验证器,但我想在这个类中添加更多规则,例如正则表达式和数字检查和电子邮件检查。 Also the password must be eight to five characters in length with one being upper case and one lower case etc. But I can't add more rules in this class.
此外,密码长度必须为 8 到 5 个字符,一个大写,一个小写等等。但我不能在这个类中添加更多规则。
I added a max length rule but it is not working.
我添加了最大长度规则,但它不起作用。
<?php
class Validator {
// for form fields storing
private $fields = array();
//for storing errors for form fields
private $field_errors = array();
private $form_is_valid = true;
public function add_field($field_name){
$this->fields[]= $field_name;
//associative array6
$this->field_errors[$field_name] = array();
}
public function add_rule_to_field( $field_name, $field_rule){
$rule_name = $field_rule[0];
switch ($rule_name)
{
case 'min_length':
if (strlen($_POST[$field_name]) < $field_rule[1]){
$this->add_error_to_field($field_name, ucwords($field_name). " cannot be less than {$field_rule[1]} in length");
//echo "must 1 chr <br>";
}
break;
case 'empty':
if(strlen($_POST[$field_name]) == 0){
$this->add_error_to_field($field_name, ucwords($field_name). " cannot be empty");
//echo "not empty <br>";
}
break;
case 'max_legnth':
if(strlen($_POST[$field_name]) > $field_rule[2]){
$this->add_error_to_field($field_name, ucwords($field_name). " cannot be greater then {$field_rule[2]} in length");
}
break;
default:
break;
}
}
private function add_error_to_field($field_name, $error_message){
$this->form_is_valid = false;
$this->form_is_valid ;
$this->field_errors[$field_name][] = $error_message;
//echo "add error to filed is working <br>";
}
public function form_valid(){
return $this->form_is_valid;
}
public function out_field_error($field_name){
if (isset($this->field_errors[$field_name])){
//echo "out ";
foreach ($this->field_errors[$field_name] as $field_errors){
echo "<p class='error'> {$field_errors} </p>";
//echo "out is running";
}
}
}
}
?>
Here is my form and object instance of the class:
这是我的表单和类的对象实例:
<?php
require_once('validator.php');
$validator = new Validator;
if(isset($_POST['submit'])){
$validator->add_field('name');
$validator->add_rule_to_field('name', array('min_length', 5));
$validator->add_rule_to_field('name', array('empty'));
$validator->add_field('email');
$validator->add_rule_to_field('email', array('min_length', 6));
$validator->add_rule_to_field('email', array('empty'));
$validator->add_field('number');
$validator->add_rule_to_field('number', array('min_length', 8));
$validator->add_rule_to_field('number', array('empty'));
$validator->add_rule_to_field('number', array('max_length', 10));
$validator->add_field('password');
$validator->add_rule_to_field('password', array('min_length', 8));
$validator->add_rule_to_field('password', array('empty'));
$validator->filed_out();
if ($validator->form_valid() == true){
echo " registration is sucessfull";
exit();
}
// else{
// echo "form is not valid";
// //exit();
// }
}
?>
<form method="POST" id="form-add" action="">
<label>Name:</label>
<input type="text" name="name" value=""/>
<span> <?php $validator->out_field_error('name'); ?></span>
<label>Email:</label>
<input type="text" name="email" value=""/>
<span><?php $validator->out_field_error('email'); ?></span>
<label>Number:</label>
<input type="text" name="number" value=""/>
<span><?php $validator->out_field_error('number'); ?></span>
<label>Password:</label>
<input type="text" name="password" value=""/>
<span><?php $validator->out_field_error('password'); ?></span>
<label>Re Enter Password:</label>
<input type="text" name="cpassword" value=""/>
<span></span>
<input type="submit" name="submit" value="SUBMIT"/>
</form>
The $rule_name
in the switch case is "max_legnth"
but should be "max_length"
. switch case 中的
$rule_name
是"max_legnth"
但应该是"max_length"
。 So you never fall in this case.所以你永远不会陷入这种情况。
Your "max_length" rule use the index 2, instead of the index 1, because $field_rule
is defined as array('max_length', 10)
.您的“max_length”规则使用索引 2,而不是索引 1,因为
$field_rule
被定义为array('max_length', 10)
。 So you have an "undefined index" notice.所以你有一个“未定义的索引”通知。
The code of the rule :规则代码:
case 'max_length':
if(strlen($_POST[$field_name]) > $field_rule[1]){
$this->add_error_to_field($field_name, ucwords($field_name). " cannot be greater then {$field_rule[1]} in length");
}
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.