3

나는 쉬운 질문을한다.Zend MultiCheckbox : 최대 선택 설정

사용자가 선택할 수있는 상자를 최대로 설정하는 기존의 zend 유효성 검사기가 있습니까? 3 상자 이상을 선택하지 않기를 바랍니다.

웹을 검색 한 결과 양식 요소의 isValid 함수에서 오류를 설정하는 것이 발견되었습니다. 그러나 그때 내가 선택한 각 상자에 대한 오류가 표시되는 문제가있어. (그래서 4 번 이상) 아니면이 문제를 어떻게 다룰 줄 아는 사람이 있을까요? 이 오류를 한 번만 표시 할 수 있다면 문제가 해결 될 것입니다.

도움 주셔서 감사합니다.

+0

아마도 그룹으로 묶어서 빠르게 작성한 그룹의 유효성 검사기를 추가하십시오. 각 요소 대신 그룹과 함께 오류를 표시하십시오. – hakre

답변

3

내 유효성 검사기를 사용할 수 있으며 값의 수를 확인합니다. 나는 정확히 같은 목적으로 - multiselect에서 선택된 값의 최대 값과 최소값을 확인하는 데 사용되었습니다.

<?php 
class App_Validate_ValuesNumber extends Zend_Validate_Abstract 
{ 
    const TOO_LESS = 'tooLess'; 
    const TOO_MUCH = 'tooMuch'; 

    protected $_type = null; 
    protected $_val = null; 

    /** 
    * @var array 
    */ 
    protected $_messageTemplates = array(
     self::TOO_LESS => "At least %num% values required", 
     self::TOO_MUCH => "Not more then %num% required", 
    ); 

    /** 
    * @var array 
    */ 
    protected $_messageVariables = array(
     'num' => '_val' 
    ); 
    /** 
    * Constructor for the integer validator 
    * 
    * @param string $type Comparison type, that should be used 
    *      TOO_LESS means that value should be greater then items number 
    *      TOO_MUCH means opposite 
    * @param int $val Value to compare items number with 
    */ 
    public function __construct($type, $val) 
    { 
     $this->_type = $type; 
     $this->_val = $val; 
    } 

    /** 
    * Defined by Zend_Validate_Interface 
    * 
    * Returns true if and only if $value is a valid integer 
    * 
    * @param string|integer $value 
    * @return boolean 
    */ 
    public function isValid($value) 
    { 
     // Value shoul dbe greated 
     if ($this->_type == self::TOO_LESS) { 
      if (count($value) < $this->_val) { 
       $this->_error(self::TOO_LESS); 
       return false; 
      } 
     } 

     // Value should be less 
     if ($this->_type == self::TOO_MUCH) { 
      if (count($value) > $this->_val) { 
       $this->_error(self::TOO_MUCH); 
       return false; 
      } 
     } 
     return true; 
    } 
} 
+0

직접 유효성 검사기를 작성하는 것도 물론 좋은 해결책입니다! 검사기가 모든 단일 체크 상자에서 실행되는 일부 변경 작업을 수행했습니다. 그래서 $ 값은 항상 단지 하나의 값을 포함합니다. 대신 $ context 매개 변수를 사용하여 값을 가져 왔습니다. – Tim

+0

이 발리 데이터를 호출하는 방법은 무엇입니까? – JellyBelly

+0

'$ this-> someFormElement -> -> addValidator (새 App_Validate_ValuesNumber (App_Validate_ValuesNumber :: TOO_LESS, 3)) ' 3 개 이하의 값을 선택해야 함 –

1

방금 ​​오늘이 값을 치렀습니다. 그것은 젠드 버그입니다. http://framework.zend.com/issues/browse/ZF-11667. 그 문제의 수정을위한 diff가 있지만, 1.12가 나오기 전까지는 수정되지 않습니다. Zend_Form_Element를 패치 할 때까지 기다리는 것을 원하지 않았습니다. 수정 프로그램은 훌륭하게 작동합니다. 수정하기 전에 MultiChecks의 내 오류 메시지는 검사 된 모든 상자에 대해 한 번 반복되었습니다.