isSubFormValid
將返回true,如果至少有一個子窗體有效。您可以指定子表單名稱或通過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;
}
/* ... */
十分感謝。它會像樹形結構一樣工作嗎?例如主窗體(array(subform1(array(subform3,subform4)),subform2(array(subform5,subform6))))? – masteryoda