इस 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);
स्रोत
2010-03-09 19:14:41