<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Antoine &#187; Validate Helper</title>
	<atom:link href="http://www.chrisantoine.com/tag/validate-helper/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrisantoine.com</link>
	<description>PHP / MySQL / JavaScript / Ajax Development.</description>
	<lastBuildDate>Fri, 13 Nov 2009 03:11:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>UniqueField Validate Helper</title>
		<link>http://www.chrisantoine.com/2008/07/14/uniquefield-validate-helper/</link>
		<comments>http://www.chrisantoine.com/2008/07/14/uniquefield-validate-helper/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 03:56:30 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Helpers]]></category>
		<category><![CDATA[Validate Helper]]></category>

		<guid isPermaLink="false">http://www.chrisantoine.net/?p=21</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <img src='http://www.chrisantoine.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ) in the 3 or 4th project that I should take advantage of the custom validators that I had read about but hadn&#8217;t tackled.</p>
<p>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.</p>
<p>To add the validator to a field using Zend Config you would do the following:</p>
<pre>
        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"
</pre>
<pre>
<code class="php">
< ?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;
    }
}
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisantoine.com/2008/07/14/uniquefield-validate-helper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

