2011-05-13 5 views
0

나는 데이터 유효성 검사 클래스 메서드를 사용하여 레코드를 데이터베이스에 삽입하기 전에 사용자 입력을 확인합니다. 내가다중 차원 배열을 사용하여 데이터 유효성 검사

필드 전자 메일이 는 다음의 유효성을 검사해야합니다 예를 들어 필드 방법에 따라 많은 규칙을 적용하고자하는

A) FILTER_VALIDATE_EMAIL

B) 중복 된 메일에 존재하는지 확인을 사용하여 이메일을 확인 데이터베이스 (이 매번 적용되지 않습니다)

필드 이름을 확인해야한다 다음과 같은.

공간 만 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.' 
     ) 
    )   
); 

다음 코드와 호환되도록 mt 코드를 구현하는 방법에 대해 혼란스러워합니다. 내 원본에 관해서는 누군가가 나중에 검증을 구현하는 것에 대한 예를 보여 주면 고맙겠습니다.

감사합니다.

답변

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 
} 
관련 문제