Last active
August 29, 2015 14:03
-
-
Save webdevilopers/1366f13a93ecf61195c2 to your computer and use it in GitHub Desktop.
ZF2 How to add fieldset to form and remove element from it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Form; | |
use Zend\Form\Form; | |
use Zend\ServiceManager\ServiceLocatorAwareInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class CreateContractForm extends Form | |
implements ServiceLocatorAwareInterface | |
{ | |
protected $serviceLocator; | |
public function init() | |
{ | |
$sm = $this->getServiceLocator(); | |
$contractFieldset = $sm->get('ContractFieldset'); | |
$contractFieldset->setUseAsBaseFieldset(true); | |
$this->add($contractFieldset); | |
// Remove `comment` element from `ContractFieldset` | |
$contractFieldset->remove('branch'); | |
} | |
public function getServiceLocator () | |
{ | |
return $this->serviceLocator; | |
} | |
public function setServiceLocator (ServiceLocatorInterface $serviceLocator) | |
{ | |
$this->serviceLocator = $serviceLocator; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Form; | |
use Zend\Form\Form; | |
class EditContractForm extends Form | |
{ | |
public function init() | |
{ | |
$this->add(array( | |
'type' => 'ContractFieldset', | |
'name' => 'contract', | |
'options' => array( | |
'use_as_base_fieldset' => true, | |
), | |
)); | |
// Remove `comment` element from `ContractFieldset` | |
$this->get('contract')->remove('branch'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my #Form I have #Fieldset named 'contract'. I would like to remove an element 'branch' that is added inside this Fieldset.
CreateContractForm.php shows the easy way when adding Fieldset via:
But how to remove the element when the Fieldset was added the way in EditContractForm.php?