2011-05-13 73 views
0

我有一个数据验证类方法,在将记录插入数据库之前检查用户输入。我想申请每场法许多规则,例如使用多维数组进行数据验证

场电子邮件将验证 以下

a)使用 FILTER_VALIDATE_EMAIL

二)检查,如果重复的邮件中存在验证电子邮件 数据库(这并不适用 每次)

字段名应该有验证下列。

a)仅AZ或az与空间中 允许

B)应该是最小的5首最大 40个字符

等我想确保我应该能够在每个字段应用许多规则,并且它也应该是可选的。

这里是我使用的原始代码。

public function validate() { 
    if(!empty($this->name)) { 
     if(!preg_match('/^[a-zA-z ]{3,50}$/',$this->name)) { 
      $this->error['name'] = 'Name should be valid letters and should be between 3 and 25 characters'; 
     } 
    } 
    if(!empty($this->email)) { 
     if(!filter_var($this->email,FILTER_VALIDATE_EMAIL)) { 
      $this->error['invalidEmail'] = 'Invalid email address'; 
     } 
     if($this->emailCount($this->email)) { 
      $this->error['emailExist'] = 'Email already exist'; 
     } 
    } 
    if(!empty($this->password)) { 
     $this->password = trim($this->password); 
     if(strlen($this->password) < 5 || strlen($this->password > 40)) { 
      $this->error['password'] = 'Password length should be between 5 and 40 characters'; 
     } 
    } 
    if(!empty($this->pPhone)) { 
     if(!preg_match('/^[0-9]{5,10}$/',$this->pPhone)) { 
      $this->error['invalidpPhone'] = 'Invalid primary phone number'; 
     } 
    } 
    if(!empty($this->sPhone)) { 
     if(!preg_match('/^[0-9]{5,10}$/',$this->sPhone)) { 
      $this->error['invalidsPhone'] = 'Invalid secondary phone number'; 
     } 
    } 
    return (empty($this->error)) ? true : false;  
} 

而不是使用很多if条件我想使用类似这样的多维数组的开关情况。

var $validate = array(
    'name' => array(
     'notEmpty'=> array(
      'rule' => 'notEmpty', 
      'message' => 'Name can not be blank.' 
     ), 
     'allowedCharacters'=> array(
      'rule' => '|^[a-zA-Z ]*$|', 
      'message' => 'Name can only be letters.' 
     ), 
     'minLength'=> array(
      'rule' => array('minLength', 3), 
      'message' => 'Name must be at least 3 characters long.' 
     ), 
     'maxLength'=> array(
      'rule' => array('maxLength', 255), 
      'message' => 'Name can not be longer that 255 characters.' 
     ) 
    ), 
    'email' => array(
     'email' => array(
      'rule' => 'email', 
      'message' => 'Please provide a valid email address.' 
     ), 
     'isUnique' => array(
      'rule' => 'isUnique', 
      'message' => 'This E-mail used by another user.' 
     ) 
    )   
); 

我越来越糊涂就如何落实万吨代码为与后一个兼容。关于我的原创,我会很感激,如果有人向我展示一个关于使用后者进行验证的例子。

谢谢。

回答

0

我会把这为一些功能:

// Validation methods 
function checkMail($mail) { 
    // perform mail checks 
    if(!valid) 
     throw Exception("not valid mail"); 
} 

function checkMinLength($string) { 
    // perform length 
    if(!valid) 
     throw Exception("not valid length"); 
} 

// mapping fields - methods 
$mappingArray = array('mailfield' => array('checkMail'), 'lengthfield' => array('checkMinLength'); 

// perform checking 
try { 
    foreach($arrayContaintingYourFields as $field) { 
     foreach($mappingArray[$field['name']] as $check) { 
     call_user_func($check, $field['value']); 
     } 
    } 
} catch (Exception $e) { 
    echo $e->getMessage(); 
} 

您可以通过定义自己的exception types并以不同的方式影响反应的错误处理。

} catch (MailException $e) { 
    echo $e->getMessage(); 
} catch (LengthException $e) { 
    // do some other stuff 
}