UniqueField Validate Helper

The last 4 projects that I have created I have built using Zend Framework. The more I use it the more I find things that make my life easier. My first shot at creating a validator was for checking whether or not a username is unique. . I need this functionality in pretty much every application I build. I realized while writing the same code again(more or less a copy and paste but still :-) ) in the 3 or 4th project that I should take advantage of the custom validators that I had read about but hadn’t tackled.

The following is probably revision 2 of the original idea. I realized after writing it the first time I should have made it more generic so I could use it on multiple fields without changing anything except the properties in the config ini for each form field.

To add the validator to a field using Zend Config you would do the following:

        form.elements.email.options.validators.uniqueField.validator = "uniqueField"
        form.elements.email.options.validators.uniqueField.options.modelName = "Notification"
        form.elements.email.options.validators.uniqueField.options.fieldname = "notification_email"

< ?php
class Robyn_Validate_UniqueField extends Zend_Validate_Abstract
{
    /**
     * @var String
     */
    const NOT_UNIQUE = 'error';

    /**
     * @var String
     */
    protected $_modelName = 'User';

    /**
     * @var String
     */
    protected $_fieldName = 'user_username';

    /**
     * @var array
     */
    protected $_messageTemplates = array(
                                         self::NOT_UNIQUE => ‘The data given is not unique.’
                                         );

    /**
     * Sets validator options
     *
     * @param  string $modelName
     * @param  string $fieldName
     * @return void
     */
    public function __construct($modelName = ‘User’, $fieldName = ‘user_username’)
    {
        $this->setModelName($modelName);
        $this->setFieldName($fieldName);
    }

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if the email address is found to NOT have a match in the fieldname in the model specified
     *
     * @param Array $value
     * @param Array $request
     * @return boolean
     */
    public function isValid($value, $request = null)
    {
        // Set the value
        $value = (string) $value;
        $this->_setValue($value);

        // Get our model and do a search for that username
        $table = new $this->_modelName();
        $result = $table->fetchAll($table->select()->where($this->_fieldName . ‘ = ?’, $value))->toArray();

        // Check to see if we got any results
        if (count($result) == 0) {
            return true;
        }

        $this->_error(self::NOT_UNIQUE);
        return false;
    }

    /**
     * Set the model name.
     *
     * @param string $modelName
     * @return Zend_Validate_UniqueUsername Provides a fluent interface
     */
    public function setModelName($modelName)
    {
        $this->_modelName = $modelName;

        return $this;
    }

    /**
     * Set the field name.
     *
     * @param string $fieldName
     * @return Zend_Validate_UniqueUsername Provides a fluent interface
     */
    public function setFieldName($fieldName)
    {
        $this->_fieldName = $fieldName;

        return $this;
    }
}

Leave a Reply