isSubFormValid
如果至少有一个子表单有效,则返回 true。您可以指定子表单名称或 pass null
。
class My_Form extends Zend_Form
{
public function isSubFormValid($name = null, array $data = null)
{
if (is_null($name)) {
$subForms = $this->getSubForms();
} else {
$subForms = array($this->getSubForm($name));
}
foreach ($subForms as $subForm) {
if ($subForm->isValid($data)) {
return true;
}
}
return false;
}
}
使用示例:
class Example extends My_Form
{
public function init()
{
$subForm1 = new Zend_Form_SubForm();
$subForm1->addElement($this
->createElement('text', 'name')
->setRequired(true));
$subForm2 = new Zend_Form_SubForm();
$subForm2->addElement($this
->createElement('text', 'name')
->setRequired(true));
$this->addSubForm($subForm1, 'form1');
$this->addSubForm($subForm2, 'form2');
$this->addElement($this->createElement('submit', 'send'));
}
}
/* ... */
public function indexAction()
{
$form = new Example();
if ($this->_request->isPost()) {
if ($form->isSubFormValid(null, $this->_request->getPost())) {
die('is valid');
}
}
$this->view->form = $form;
}
/* ... */