2013-02-21 2 views

답변

1

그냥 유효성 검사기의 isValid 메서드는 유효성 검사기가 연결된 필드의 값과 다른 모든 양식 필드의 값 배열 인 양식의 전체 컨텍스트 값을 가져옵니다. 추가적으로 컨텍스트 필드를 설정하는 검사기에 기능을 추가하고, 심지어 더 적은 같 더 이상되어야 하는지를 비교하는 기능 ... 여기

(안 시험) 일부 예 코드

<?php 

namespace My\Validator; 

class MinMaxComp extends AbstractValidator 
{ 
    const ERROR_NOT_SMALLER = 'not_smaller'; 
    const ERROR_NOT_GREATER = 'not_greater'; 
    const ERROR_CONFIG = 'wrong_config'; 

    const TYPE_SMALLER = 0; 
    const TYPE_GREATER = 1; 

    protected $messageTemplates = array(
     self::ERROR_NOT_SMALLER => "Blah", 
     self::ERROR_NOT_GREATER => "Blah", 
     self::WRONG_CONFIG => "Blah", 
    ); 

    protected $type; 
    protected $contextField; 

    public function setType($type) 
    { 
     $this->type = $type; 
    } 

    public function setContextField($fieldName) 
    { 
     $this->contextField = $fieldName; 
    } 

    public function isValid($value, $context = null) 
    { 
     $this->setValue($value); 

     if (!is_array($context) || !isset($context[$this->contextField])) { 

      return false; 
     } 

     if ($this->type === self::TYPE_SMALLER) { 

      if (!$result = ($value < $context[$this->contextField])) { 

       $this->error(self::ERROR_NOT_SMALLER); 
      } 

     } else if ($this->type === self::TYPE_GREATER) { 

      if (!$result = ($value > $context[$this->contextField])) { 

       $this->error(self::ERROR_NOT_GREATER); 
      } 

     } else { 

      $result = false; 
      $this->error(self::ERROR_CONFIG); 
     } 

     return $result; 
    } 
} 
전달할 수
3

사용자 지정 유효성 검사를 만들어야합니다.

이 게시물의 기능은 My_Validate_FieldCompare에 도움이 될 것이라고 생각합니다.