Studyon Minte9.com
ZF 1.0




View script



#modules/default/controllers/FrontendController.php
<?php
	...
	public function loginAction() {

		$form = new Forms_Login();
		$form->setDecorators(array(array('ViewScript', array('viewScript' => 'frontend/login_form.phtml'))));
		
		if ($this->_request->isPost()) {
			if ($form->isValid($this->_request->getParams())) {
				// do whatever you need to do
			}
		}

		$this->view->loginForm = $form;

	}

#modules/default/Forms/Login.php
<?php
	class Forms_Login extends Zend_Form {
		
		public function init() {
			
			$username = $this->createElement('text','username');
			$username->setRequired(true);
			$username->setLabel('Username');
			
			$password = $this->createElement('password','password');
			$password->setRequired(true);
			$password->setLabel('Password');
			
			$submit = new Zend_Form_Element_Submit("submit");
			$submit->setLabel("Submit");

			$this->addElements(array($username, $password, $submit));

			foreach($this->getElements() as $element) {
				$element->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
			}
		}
	}

#modules/default/views/scripts/frontend/login.phtml
<?php 
	echo $this->loginForm;


#modules/default/views/scripts/frontend/login_form.phtml
<table>
<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()) ?>">
<tr>
	<td id="username">Username</td>
	<td><?php echo $this->element->username; ?></td>
</tr>
<tr>
	<td id="password">Password</td>
	<td>
		<?php echo $this->element->password; ?>
	</td>
</tr>
<tr>
	<td></td>
	<td><input type="submit" name="submit" id="submit" value="Submit"></td>
</tr>
</form>
</table>

<div class="errors">
	<?php foreach($this->element->getMessages() as $k=>$v): ?>
		<?php echo $k; ?><br>
			<?php foreach($v as $vv): ?>
				&nbsp; <?php echo $vv; ?><br>
			<?php endforeach; ?>
	<?php endforeach; ?>
</div>


http://www.zendframeworkexpert.co.uk/2009/06/09/using-a-view-script-to-render-a-zend_form_element/
http://framework.zend.com/manual/en/zend.form.standardDecorators.html#zend.form.standardDecorators.viewScript