page의 '참고 : 유효성 확인 컨텍스트'를 참조하십시오. Zend_Form은 모든 Zend_Form_Element :: isValid 호출에 따라 컨텍스트를 두 번째 매개 변수로 전달합니다. 따라서 컨텍스트를 분석하는 자체 유효성 검사기를 작성하십시오.
편집 :
좋아, 나는 I'ld이 나 자신을 탄을 생각했다. 그것은 테스트되지도 않았고 모든 것을 의미하는 것이 아니라 기본적인 아이디어를 줄 것입니다.
class My_Validator_OneFieldShouldBePresent extend Zend_Validator_Abstract
{
const NOT_PRESENT = 'notPresent';
protected $_messageTemplates = array(
self::NOT_PRESENT => 'Field %field% is not present'
);
protected $_messageVariables = array(
'field' => '_field'
);
protected $_field;
protected $_listOfFields;
public function __construct(array $listOfFields)
{
$this->_listOfFields = $listOfFields;
}
public function isValid($value, $context = null)
{
if(!is_array($context))
{
$this->_error(self::NOT_PRESENT);
return false;
}
foreach($this->_listOfFields as $field)
{
if(isset($context[ $field ]))
{
return true;
}
}
$this->_field = $field;
$this->_error(self::NOT_PRESENT);
return false;
}
}
사용법 :
$oneOfTheseFieldsShouldBePresent = array('companyname', 'companyother');
$companyname = new Zend_Form_Element_Text('companyname');
$companyname->setLabel('Company Name');
$companyname->setDecorators($decors);
$companyname->addValidator(new My_Validator_OneFieldShouldBePresent($oneOfTheseFieldsShouldBePresent));
$this->addElement($companyname);
$companyother = new Zend_Form_Element_Text('companyother');
$companyother->setLabel('Company Other');
$companyother->setDecorators($decors);
$companyname->addValidator(new My_Validator_OneFieldShouldBePresent($oneOfTheseFieldsShouldBePresent));
$this->addElement($companyother);