Vedere "Nota: contesto di convalida" al numero page. Zend_Form passa il contesto ad ogni Zend_Form_Element :: isValid call come secondo parametro. Quindi scrivi semplicemente il tuo validatore che analizza il contesto.
EDIT:
Va bene, ho pensato Se mai prendere un colpo a questo me stesso. Non è testato, né è un mezzo per tutti i fini, ma ti darà un'idea di base.
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;
}
}
Usage:
$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);
fonte
2010-03-09 19:14:41