表单的PHP验证 - preg_match

时间:2015-04-06 20:04:59

标签: php validation

我建立了一个名为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);
     }
}


?>

1 个答案:

答案 0 :(得分:0)

//只传递数组中的regexp,而不是那里总是为true的preg_match

'special' => '/[a-zA-Z0-9 ]/' 

protected function special($field, $value, $satisfier){
        return preg_match($satisfier, $value);
}