Created
March 6, 2014 07:58
-
-
Save dmitrybelyakov/9384493 to your computer and use it in GitHub Desktop.
Bind entity to form with fieldsets
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 | |
/** | |
* Example action | |
* This is an example action that tests binding of forms to entities. | |
* @return \Zend\View\Model\ViewModel | |
*/ | |
public function exampleAction() | |
{ | |
$node = new ArrayObject(array( | |
'id' => 9, | |
'locale' => 'en_US', | |
'status' => 'published', | |
'title' => 'Test', | |
'slug' => 'test-item', | |
)); | |
$form = new \Zend\Form\Form('example'); | |
//meta | |
$meta = new \Zend\Form\Fieldset('Meta'); | |
$meta->add(new \Zend\Form\Element\Text('title', ['label' => 'Title'])); | |
$form->add($meta); | |
//general | |
$general = new \Zend\Form\Fieldset('General'); | |
$general->add(new \Zend\Form\Element\Text('slug', ['label' => 'Slug'])); | |
$form->add($general); | |
$form->bind($node); | |
$response['form'] = $form; | |
$vm = new ViewModel(array('form' => $form)); | |
$vm->setTemplate('example'); | |
return $vm; | |
} |
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 $this->form->prepare(); ?> | |
<?= $this->form()->openTag($this->form) ?> | |
<?php foreach ($this->form as $elementOrFieldset): ?> | |
<?php if ($elementOrFieldset instanceof Zend\Form\Fieldset): ?> | |
<?= $this->formCollection($elementOrFieldset) ?> | |
<?php else: ?> | |
<?= $this->formRow($elementOrFieldset) ?> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
<?= $this->form()->closeTag() ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment