2015-04-06 82 views
1

我设置了一个名为Validator的类,用于验证放入表单的输入用户。这是基本的,现在它正在检查,以确保没有留下空白,最小长度和最大长度。我想弄清楚如何检查并且不允许特殊字符但是不成功。我曾尝试添加pregmatch,但我正在实现它不正确,或者我只是不能通过如何设置我的代码来实现它?一些反馈将会有所帮助,并提前致谢。PHP验证表单 - 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); 
    } 
} 


?> 

回答

0

//通过正则表达式只是在阵列中,没有的preg_match总是真有

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

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