isSubFormValid
少なくとも1つのサブフォームが有効な場合、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;
}
/* ... */