Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/1366f13a93ecf61195c2 to your computer and use it in GitHub Desktop.
Save webdevilopers/1366f13a93ecf61195c2 to your computer and use it in GitHub Desktop.
ZF2 How to add fieldset to form and remove element from it
<?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;
}
}
<?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');
}
}
@webdevilopers
Copy link
Author

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:

$contractFieldset = new ContractFieldset();

But how to remove the element when the Fieldset was added the way in EditContractForm.php?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment