Created
July 1, 2012 16:41
-
-
Save bakura10/3028917 to your computer and use it in GitHub Desktop.
Multiple hydrator usage
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
class CreateForm extends \Zend\Form\Form | |
{ | |
public function __construct() | |
{ | |
parent::__construct('create_address'); | |
$this->setAttribute('method', 'post') | |
->setHydrator(new ClassMethodsHydrator()) | |
->setInputFilter(new InputFilter()); | |
// It would be interesting to have validation group per fieldset instead of per form, as it would allow to completely reuse | |
// fieldset accross different forms | |
$address = new AddressFieldset(); | |
$address->setUseAsBaseFieldset(true); | |
$this->add($address); | |
$this->add(array( | |
'name' => 'submit', | |
'attributes' => array( | |
'type' => 'submit' | |
) | |
)); | |
} | |
WHERE ADDRESSFIELDSET : | |
class AddressFieldset extends Fieldset implements InputFilterProviderInterface | |
{ | |
public function __construct() | |
{ | |
parent::__construct('address'); | |
$this->setHydrator(new ClassMethodsHydrator()) | |
->setObject(new \Application\Entity\Address()); | |
$street = new \Zend\Form\Element('street'); | |
$street->setAttributes(array( | |
'type' => 'text', | |
'label' => 'Street' | |
)); | |
$city = new \Application\Form\CityFieldset; | |
/*$city->setAttributes(array( | |
'label' => 'City' | |
));*/ | |
$this->add($street); | |
$this->add($city); | |
} | |
/** | |
* Should return an array specification compatible with | |
* {@link Zend\InputFilter\Factory::createInputFilter()}. | |
* | |
* @return array | |
*/ | |
public function getInputFilterSpecification() | |
{ | |
return array( | |
'street' => array( | |
'required' => true, | |
) | |
); | |
} | |
} | |
City Fieldset and Country Fieldset as built the same way (but with different object given to setObject function). | |
The view : | |
<?php | |
$address = $form->get('address'); | |
echo $this->formRow($address->get('street')); | |
$city = $address->get('city'); | |
echo $this->formRow($city->get('name')); | |
echo $this->formRow($city->get('zipCode')); | |
$country = $city->get('country'); | |
echo $this->formRow($country->get('name')); | |
echo $this->formRow($country->get('continent')); | |
?> | |
Controller : | |
$form = new \Application\Form\CreateForm(); | |
$address = new \Application\Entity\Address(); | |
$form->bind($address); | |
$form->setBindOnValidate(false); | |
if ($this->request->isPost()) { | |
$form->setData($this->request->getPost()); | |
if ($form->isValid()) { | |
var_dump($address); | |
} | |
} | |
$address is : | |
object(Application\Entity\Address)[597] | |
protected 'street' => string 'zeg' (length=3) | |
protected 'city' => | |
object(Application\Entity\City)[583] | |
protected 'name' => string 'grt' (length=3) | |
protected 'zipCode' => string 'ferg' (length=4) | |
protected 'country' => | |
object(Application\Entity\Country)[589] | |
protected 'name' => string 'rthr' (length=4) | |
protected 'continent' => string 'hthty' (length=5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should read setBindOnValidate(true), sorry :).