Zend framework - is there a validator for multiple checkboxes?
I want 2 checkboxes for yes/no answers, and require that either one be 开发者_C百科ticked. Is there a built-in validator for a 'group' of checkboxes?
As far as I know there isn't.
But you can do something like this in your form. Manually inside your form class, extend the isValid method to check that at least checkbox_1 or checkbox_2 is ticket. And you should do what Adrian said use radio instead. But the principle is the same.
<?php
class App_Your_Form extends Zend_Form
{
public function init()
{
... Your stuff ...
}
public function isValid($data)
{
$isValid = parent::isValid($data);
if ($this->getValue('checkbox_1') != '1' && $this->getValue('checkbox_2') != '1') {
$this->getElement('checkbox_1')->setErrors(array('You have to set check at least chexbkox_1 or checkbox 2'));
$isValid = false;
}
return $isValid;
}
}
If you use the radio element you only need setRequired(true)
. Same actually works for the MultiCheckbox
Zend element.
$element->setRequired(true);
精彩评论