Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This form class demonstrates how to get default values for form fields
- * from an object with getters.
- * @author Gabriel Birke <birke@d-scribe.de>
- */
- class Example_Form extends Zend_Form {
- // Set up the form fields
- function init() {
- $this->addElement('text', 'name', array('label' => 'Name'));
- $this->addElement('text', 'street', array('label' => 'Street'));
- $this->addElement('text', 'city', array('label' => 'City'));
- $this->addElement('text', 'zipcode', array('label' => 'ZIP'));
- $this->addElement('checkbox', 'spamLikeThereIsNoTomorrow',
- array('label' => 'Send me a newsletter'));
- $this->addElement('checkbox', 'sucker',
- array('label' => 'I have read the terms and conditions'));
- $this->addElement('submit', 'submit', array('label' => 'Order now'));
- }
- public function setDefaultsFromObject($object)
- {
- $reflector = new ReflectionObject($object);
- $fields = array_keys($this->getElements());
- foreach($fields as $field) {
- $method = 'get'.ucfirst($field);
- if($reflector->hasMethod($method)) {
- $this->setDefault(
- $field,
- $reflector->getMethod($method)->invoke($object)
- );
- }
- }
- return $this; // fluent interface
- }
- }
Add Comment
Please, Sign In to add comment