views:

33

answers:

3

Hi, I have a form with several subforms. I've overriden the form's isValid function with my own, but can't find any documentation on how to set an isValid function per individual subform.

Can someone post a link or sample code so I can learn how to do this please.

Thanks in advanced.

+1  A: 
$form1 = new ..._Form1();
$form2 = new ..._Form2();

if ($form1->isValid($this->getRequest()->getPost())) {
   ...
}

if ($form2->isValid($this->getRequest()->getPost())) {
   ...
}
Alexander.Plutov
+1  A: 
<?php
class Your_Sub_Form extends Zend_Form_SubForm
{
    public function isValid($data)
    {
        // Your custom validation-logic here
        return parent::isValid($data);
    }

    public function init()
    {
        ...
    }
}

class Your_Form extends Zend_Form
{
    public function isValid($data)
    {
        return parent::isValid($data);
    }

    public function init()
    {
        $this->addSubForm(new Your_Sub_Form(), 'subform');

        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Submit',
        ));
    }
}
Benjamin Cremer
A: 

Try using Zend_Validate_Callback http://framework.zend.com/manual/en/zend.validate.set.html to validate just the special fields you want. You can access other field values through the context.

Steve