Tag: Zend Framework
Zend_Form_Decorator_ViewScript
by Daniel on Dec.08, 2009, under Blog
Ok, not exactly breaking news, but I’ve just discovered the Zend_Form_Decorator_ViewScript class, the Zend DevZone article covering it, well, it helped me find it, but the examples are pretty useless if you want it to actually work! (No surprises there then, sorry DevZone.)
So to that end I thought I’d blog.
Zend Form Decorators can be hard enough to twist your brain around at the best of times. But the ViewScript Decorator makes forms easy again. You can apply the ViewScript decorator to any Zend_Form element, though I’d have thought that using it on the Form itself is all you’d really need.
File: ./application/forms/Example.php
<?php
/**
* ViewScript Decorator example form
*/
class Form_Example extends Zend_Form
{
/**
* Initialise
*
* Set up the form, that's down to you,
* set the ViewScript decorator for the form
* @access public
*/
public function init()
{
/* ... Initialise my form ... */
$this->setDecorators( array(
array( 'ViewScript', array( 'viewScript' => 'forms/example.phtml' ) )
) );
}
}
?>
Remember that the path forms/example.phtml is relative to the configured view script directories. So in my app these are ./application/views/scripts/:./application/modules/default/views/scripts/:./application/modules/admin/views/scripts/.
Inside the view script, the form element you’re decorating is assigned to the element view property. So inside the view script ( $this->element instanceof Zend_Form ) === true.
That just leaves you with the view script itself now. Well, they’re just like any other view script except you only have $this->element property, your view helpers remain available to you as always.
File: ./application/views/scripts/forms/example.phtml
<form id="exampleForm" action="<?php echo $this->element->getAction() ?>" method="<?php echo $this->element->getMethod() ?>">
<h2>Your Details</h2>
<?php echo $this->element->email ?>
<?php echo $this->element->dob ?>
<h2>Account Details</h2>
<?php echo $this->element->username ?>
<?php echo $this->element->password ?>
<?php echo $this->element->passwordconfirm ?>
<h2>Terms & Conditions</h2>
<p>Read our <a href="<?php echo $this->url( array(), 'terms_and_conditions' ) ?>">Terms & Conditions</a> before signing up.</p>
<?php echo $this->element->termsandconditions ?>
<?php echo $this->element->submit ?>
<p class="small">NOTE: This is where you may want reassure your visitors that you're to be trusted.</p>
</form>
Now that’s a pretty simple example of how you’d use the ViewScript decorator. You may be able to achieve that markup using only the other decorators. I don’t know, and I can’t really be arsed enough to find out. I plan to just keep it simple and have a view script for each form that needs it, and use other decorators for the individual form elements.
It is now down to you to build the <form/> element, the individual form elements are still automatically generated though so you still get all those fancy features that made you want Zend_Form in the first place. Simply echo them out.
Main Config with Zend_Application
by Daniel on Nov.02, 2009, under Programming
Mainly a note to myself, but it’s handy to know.
When you instanciate Zend_Application, you provide it with a path to the config file (Or an instance if you’re feelin’ kinky) Zend_Application loads this into a Zend_Config object and does it’s bit with it.
What’s less obvious is how to retrieve that config from within a Zend_Controller_Action::method().
My solution until recently was to have an _initConfig() in my bootstrap class that read in the file again, and stored the instance in a Zend_Registry entry. However I think I cracked the “proper” way or “better” way to do it.
<?php
class MyController extends Zend_Controller_Action
{
/**
* @var array The application configuration
* @access protected
*/
protected $_config;
/**
* Init
*
* Retrieve and assign the application configuration
*
* @access public
* @return null
*/
public function init()
{
$this->_config = Zend_Controller_Front::getInstance()
->getParam( 'bootstrap' )
->getOptions();
}
}
?>
Edit: In that particular example there is actually an instance of Zend_Controller_Front closer to hand, you could use $this->_frontController. But I’ll stick with ::getInstance() for the example since since it gets the job done.