表单预匹配的PHP验证


PHP Validation for form - pregmatch

我建立了一个名为Validator的类,用于验证用户输入到表单中的内容。它现在是基本的,它正在检查以确保没有任何内容为空,最小长度和最大长度。我正试图找出如何检查和不允许特殊字符,但一直没有成功。我尝试过添加pregmatch,但我实现错误,或者我无法通过设置代码来实现它?一些反馈会很有帮助,并提前感谢您。

这是我的验证器文件中的代码

<?php

class Validator
{
    protected $errorHandler;
      protected $rules = ['required', 'minlength', 'maxlength' ]; //special is new -'special'
      public $messages = [
          'required' => 'The :field field is required',
          'minlength' => 'The :field field must be a minimum of :satisfier length',
          'maxlength' => 'The :field field must be a maximum of :satisfier length',
          // 'special' => 'The :field field cannot contain special characters or spaces', 
      ];
    public function __construct(ErrorHandler $errorHandler) // before contstruct there are (2) __ not one _
    {
        $this->errorHandler = $errorHandler;
    }
    public function check($items, $rules)
    {
        foreach($items as $item => $value)
        {
            if(in_array($item, array_keys($rules)))
            {
                $this->validate([
                    'field' => $item,
                    'value' => $value,
                    'rules' => $rules[$item]
                ]);
            }
        }
         return  $this;
    }
    public function fails()
    {
        return $this->errorHandler->hasErrors();
    }

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

    protected function validate($item)
    {
        $field = $item['field'];
        foreach($item['rules'] as $rule => $satisfier)
        {
            if(in_array($rule, $this->rules))
            {

            if(!call_user_func_array([$this, $rule], [$field, $item['value'], $satisfier]))
                {
                    $this->errorHandler->addError(
                        str_replace([':field', ':satisfier'], [$field, $satisfier], $this->messages[$rule]), 
                        $field);
                    }
                }
            }
        }

protected function required($field, $value, $satisfier)
    {
        return !empty(trim($value));
    }
    protected function minlength($field, $value, $satisfier)
    {
        return mb_strlen($value) >= $satisfier;
    }
    protected function maxlength($field, $value, $satisfier)
    {
        return mb_strlen($value) <= $satisfier;
    }
    //new special
/*
protected function special($field, $value, $satisfier){
        return preg_match(firstname)<=$satisfier;
    }
*/

}

This is the code from my form php file
<?php

require_once 'Class/ErrorHandler.php';  
require_once 'Class/Validator.php';
require_once 'insert_data.php';
$errorHandler = new ErrorHandler();
if(!empty($_POST))
{
    $validator = new Validator($errorHandler);
    $validation = $validator->check($_POST, [
    'firstname' => [
            'required' => true,
            'maxlength' => 25,
            'minlength' => 3,
            'special'=> preg_match('/[a-zA-Z0-9 ]/','firstname')//new
        ],
        'lastname' => [
            'required' => true,
            'maxlength' => 25,
            'minlength' => 2,
            'special'=> preg_match('/[a-zA-Z0-9 ]/','lastname')//new

        ],
        'password' => [
            'required' => true,
            'maxlength' => 25,
            'minlength' => 7,
            //'special'=> preg_match('/[a-zA-Z0-9 ]/','password')//new

        ]
    ]);

     if($validation->fails())
     {
        echo '<pre>', print_r($validation->errors()->all()),'</pre>';
     }
     else
     {
         insert_request($_POST);
     }
}

?>

//只在数组中传递regexp,而不是在中始终为true的preg_match

'special' => '/[a-zA-Z0-9 ]/' 
protected function special($field, $value, $satisfier){
        return preg_match($satisfier, $value);
}