使用oop php进行表单验证

时间:2018-03-01 17:42:29

标签: php oop

我想通过面向对象技术使用php验证表单。我通过观看教程创建了一个类验证器,但我想在这个类中添加更多规则,例如正则表达式和数字检查以及电子邮件检查。密码的长度也必须是8到5个字符,其中一个是大写字母,一个是小写字母等。但我不能在这个类中添加更多规则。

  

我添加了最大长度规则,但它无效。

<?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";
            }
        }
    }


}
 ?>
  

这是我的类的表单和对象实例:

<?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>

1 个答案:

答案 0 :(得分:0)

  • 切换案例中的$rule_name"max_legnth",但应为"max_length"。所以你永远不会陷入这种情况。

  • 您的“max_length”规则使用索引2而不是索引1,因为$field_rule定义为array('max_length', 10)。所以你有一个“未定义的索引”通知。

规则的代码:

        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;