Zend_Form

Custom Zend Validator error messages

The Zend Validator error messages are not the most helpful. Considor the below code for creating a file input:

$this->addElement('file', 'signature', array(
    'validators' => array(
        array('Size', false, 20480), //20k
        array('Extension', false, 'png'),
    ),
    'required' => false,
    'label' => 'Signature',
    'destination' => SIG_PATH,
));

For instance, on the above file input, if you attempt to upload something that is not a PNG file (i.e. photo.JPG, the message will be:

File 'photo.JPG' has a false extension

That’s not very user friendly as it doesn’t give the user any indication of what is an “allowed” file type.

Continue reading

Accessing controller data from a partial viewscript

When using an inline partial viewscript in my form, I sometimes need access to data. I can accomplish this by using a setter and getter with a public function in my form. We’ll use the action “guinness” as the controller action in this example.

application\modules\project\forms\guinness\base.php

protected $_id;

public function setId($id)
{
    $this->_id = $id;
    return $this;
}

public function getId()
{
    return $this->_id;
}

public function getPintCount()
{
    $model = new PubModel();
    return $model->countPints(this->getId());
}

application\modules\project\views\scripts\guinness\page.phtml

<?=$this->render('_partial.phtml');?>

application\modules\project\controllers\GuinnessController.php

<?php
$form = new SomeForm();
$form->setId(123);
?>

application\modules\projects\views\scripts\guinness\_partial.phtml

<?=$this->element->getPintCount();?>

 

This way I can get the content view my connected partials which in essence are a copy of the viewscript.

Seems a long way round, but it keeps everything nicely separated.