5

Zend Framework Sucks: One Validate Callback per Form Field

I completely loathe Zend_Form and it’s silly validation system, but I’ll save that for another time and just concentrate on a specific example: You can only have one validation callback per form element.

There’s the awesome Zend_Validate_Callback which allows us to try to make up for other validation shortcomings by allowing us to define our own validation rules.

But apparently you can only add one callback validator per form element.

Why is this an issue?

Zend_Validate_Callback provides silly error messages (“% is not valid”). So it’s desirable to override this message, but if you’re doing multiple custom validations, you’ll want multiple messages depending on how validation failed. Well, you can’t add multiple messages, so the intuitive (as close as you can get to intuitive with ZF) thing to do would be to break it out into multiple validators and add a helpful message for each. You can’t do that!

Thanks, ZF.

A possible solution would be to pass an instance of the object to the callback via setOptions(). But the messages are private, with no method to change them.

Hackish solution: create our own validator.

<?php
require_once 'Zend/Validate/Abstract.php';
require_once 'Zend/Validate/Callback.php';
 
class My_Validate_Callback extends Zend_Validate_Callback
{
 
    public function setMessage($message, $value)
    {
        if(isset($this->_messageTemplates[$message]))
            $this->_messageTemplates[$message] = $value;
    }
 
    public function isValid($value)
    {
        $this->_setValue($value);
 
        $options  = $this->getOptions();
        $callback = $this->getCallback();
        $args     = func_get_args();
        $options  = array_merge($args, $options);
 
        $options[] = &$this;
 
        try {
            if (!call_user_func_array($callback, $options)) {
                $this->_error(self::INVALID_VALUE);
                return false;
            }
        } catch (Exception $e) {
            $this->_error(self::INVALID_CALLBACK);
            return false;
        }
 
        return true;
    }
}

All we’re doing here is extending Zend_Validate_Callback and (1) adding a method to override the message templates and (2) passing a reference to this object to the callback method.

Complex. Insane. Should be unneeded. Zend Framework.

0

Omitting the Closing tag in PHP Files

At first when I saw the Zend Framework recommendation of omitting the closing tag in PHP files I thought it bizarre and stupid. Afterall, you close every other syntactic character in programming: parenthesis, brackets, HTML tags, ifs, loops, etc. So why wouldn’t you close the PHP tag at the end of the document as well?

The Zend Framework reason is, of course, to white space data from being sent to the browser prematurely, which messes up sending header information to the browser. My first thought was “this is lazy and encourages bad coding.”

But the more I think about it, the more it makes sense. It isn’t laziness, but a fool proof way to prevent a common mistake from happening. And apparently the closing tag isn’t required at all, so why not?

I don’t think I’ll be quick to adopt this practice, but maybe eventually, and maybe as I write new code. We’ll see.

Copyright © 2010 — phup 'n stuff | Site design by Trevor Fitzgerald