Search code examples
phpzend-frameworkzend-formsubform

Using custom isValid() function for specific subform in Zend_Form


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 advance.


Solution

  • <?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',
            ));
        }
    }