Decorators with Zend_Form
just turned to zend framework recently. im java programmer, who code php + zend in my spare time.
The article describe how to add decorators to a zend form. Im one of those who don't like the default - cause im trying to port my existing homepage, and ... css is time consuming.
but it also describes how to use view script with zend forms - gives you 100% control. Nice - lets begin.
Made a login form - its a rip off from a tutorial, but this is how I used quickform from pear also, so its kinda familiar.
LoginForm.php
PHP Code:
class LoginForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('loginForm');
$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$password = new Zend_Form_Element_Text('password');
$password->setLabel('Password')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$id = new Zend_Form_Element_Hidden('id');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->setDecorators( array( array('ViewScript', array('script' => 'loginForm.phtml'))));
$this->addElements(array($id, $username, $password, $submit));
}
}
setDecorators( array( array('ViewScript', array('script' => 'loginForm.phtml'))));
This confuses me, cause I haven't managed to understand what Im doing here, and what parameters I set.
setDecorators, removes all existing decorators, and takes an array of decorators ... and I specify it is an ViewScript .. ok ... script is for??? and the form ofc ...
But i get an exception error, saying: Warning: No view script registered with ViewScript decorator in C:\www\webroot\zftutorial150\library\Zend\Form.php on line 2179
I have a feeling that the form has to know the path to the viewscript ... or ...
Im kinda confused cause alot seems to work without any configuration, if not you got to configure stuff ... but how and when ...
Reference doc is confusing, api doc is microscopic
My view script...
loginForm.phtml
PHP Code:
<form action="<?= $this->escape($this->login->getAction() ?>"
method="<?= $this->escape($this->login->getMethod() ?>">
<?= $this->login->id ?>
Username<br />
<?= $this->login->username ?><br />
Password<br />
<?= $this->login->password ?><br />
<?= $this->login->submit ?>
</form>
part of IndexController.php
PHP Code:
class IndexController extends Zend_Controller_Action
{
function indexAction()
{
$this->view->title = "My Albums";
$albums = new Albums();
$this->view->albums = $albums->fetchAll();
$loginForm = new LoginForm();
$loginForm->submit->setLabel( 'Login' );
$this->view->loginForm = $loginForm;
}
}
I have a layout file where I just do this: <?php echo $this->loginForm; ?> .. and standard zend form works splendid right away ... but when I tried to make it use a viewscript it does not work.