Studyon Minte9.com
ZF 1.0




Elements



// controller
...
$form = new Forms_Update();
$form->setDecorators(array(array('ViewScript', array('viewScript' => 'useraccount/form_update.phtml'))));
		

// modules/user/Forms/Update.php
<?php
class Forms_Update extends Zfsite_Form {
	
	protected $_elements = array(
		'username' => 'text',
		'gender' => 'radio',
		'category' => 'select', 
	);
	
	public function setElement($element, $name) {
		
		switch($name) {
			
			case 'catg_id' :
		        	$modelCatg = new Models_Catg();
		        	$element->addMultiOptions($modelCatg->getCatgs());
		        break;

			case 'username': 
				$element->setRequired(true);
				$element->addValidators(array(
					'Alnum',
					array('Regex', false, array('/^[a-z][a-z0-9]{2,}$/')),
					array('StringLength', false, array('min'=>5, 'max'=>20)),
				));
			break;
			
			case 'gender':
				$translate = Zend_Registry::get('Zend_Translate');
				
				$element->setRequired(true);
				$element->addMultiOptions(array(
					'm' => $translate->translate('male'),
					'f' => $translate->translate('female'),
				));
				$element->setSeparator(''); // remove <br />
				$element->setAttrib('class', 'radio'); // OR $element->class = 'radio';
				$element->setValue(array('m'));
			break;

			case 'category' :
				$element->addMultiOptions(array(
				    '1' => '111',
				    '2' => '222',
				));

				/*
					Optgroup
					$element->addMultiOptions(
						'Choose Category' => array(
						    '1' => '111',
						    '2' => '222',
						));
				*/

			    break;

			case 'captcha': 
                                $element = new Zend_Form_Element_Captcha('captcha',
                                array(
                                'label' => "Introdu codul alaturat",
                                'captcha' => array(
                                            'captcha' => 'Image',
                                            'wordLen' => 5,
                                            'height'  => 50,
                                            'width'   => 100,
                                            'timeout' => 300,
                                            '_fsize' => 20,
                                            '_dotNoiseLevel' => 0,
                                            '_lineNoiseLevel' => 0,
                                            'font' => './lib/captcha/ttf/Vera.ttf',
                                            'imgdir'=>  './lib/captcha/tmp/',
                                            'imgurl' => '/lib/captcha/tmp/'
                                            ),
                                )
                            ); break;
			
			// end switch
		}
	}

// modules/user/view/scripts/useraccount/form_update.phtml
<fieldset>
	<legend>Modifica date cont</legend>
	
	<form enctype="<?php echo $this->escape($this->element->getEnctype()) ?>" 
		action="<?php echo $this->escape($this->element->getAction()) ?>"
		method="<?php echo $this->escape($this->element->getMethod()) ?>">
	<p>
		<label for="name"><?php echo $this->translate('username') ?></label>
		<?php echo $this->element->username; ?>
		<?php echo $this->tooltip()->set('UsernameFormat')->render(); ?>
		<?php echo $this->tooltip()->setForm($this->element)->setError('username')->render(); ?>
	</p>
	<p>
		<label for="name"><?php echo $this->translate('gender') ?></label>
		<?php echo $this->element->gender; ?>
		<?php echo $this->tooltip()->setForm($this->element)->setError('gender')->render(); ?>
		<br clear='all'>
	</p>		
	<p>
		<input type="submit" name="send" class="formbutton" value="Submit" />
	</p>
	</form>
</fieldset>


http://wesleyalmeida.net/what-ive-learned/how-to-decorate-a-radio-element-in-zend/