<?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>LAMP with ·dotmanila &#187; Zend_Validate</title>
	<atom:link href="http://dotmanila.com/blog/tag/zend_validate/feed/" rel="self" type="application/rss+xml" />
	<link>http://dotmanila.com/blog</link>
	<description>Linux, Apache, PHP, MySQL Musings</description>
	<lastBuildDate>Wed, 01 Feb 2012 23:32:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Zend_Validate_StringEquals</title>
		<link>http://dotmanila.com/blog/2010/01/zend_validate_stringequals/</link>
		<comments>http://dotmanila.com/blog/2010/01/zend_validate_stringequals/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 14:54:23 +0000</pubDate>
		<dc:creator>jervin</dc:creator>
				<category><![CDATA[Application Security]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ZendFramework]]></category>
		<category><![CDATA[Zend_Input_Filter]]></category>
		<category><![CDATA[Zend_Validate]]></category>

		<guid isPermaLink="false">http://dotmanila.com/blog/?p=121</guid>
		<description><![CDATA[If you ever wonder where that &#8216;StringEquals&#8217; validator rule taken as example from the Zend_Filter_Input documentation page results in an error like below, well read again. It was clearly stated as &#8216;hypothetical&#8217;. Plugin by name &#8216;StringEquals&#8217; was not found in the registry; used paths: Zend_Validate_: Zend/Validate/ Given such validator would be useful on a number [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever wonder where that &#8216;StringEquals&#8217; validator rule taken as example from the Zend_Filter_Input documentation page results in an error like below, well read again. It was clearly stated as &#8216;hypothetical&#8217;.</p>
<blockquote>
<h3>Plugin by name &#8216;StringEquals&#8217; was not found in the registry; used paths: Zend_Validate_: Zend/Validate/</h3>
</blockquote>
<p>Given such validator would be useful on a number of situations i.e. confirming passwords, emails, etc. I present to you my own version of the class.</p>
<pre class="brush: php;ruler: true;">
&lt;?php
"%field1% and %field2% are not equal.",
        self::MISSING	=&gt; "One or both strings are missing."
    );

    /**
     * @var array
     */
    protected $_messageVariables = array(
        'field1' =&gt; '_field1',
        'field2' =&gt; '_field2'
    );

    protected $_case = false;
    protected $_field1 = null;
    protected $_field2 = null;

    /**
     * Sets validator options
     *
     * @param  boolean $case
     * @return void
     */
    public function __construct($case = false)
    {
        $this-&gt;_case = $case;
    }

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if the the 2 strings are equal
     *
     * @param  array $value
     * @return boolean
     */
    public function isValid($value)
    {
    	if(!is_array($value) OR sizeof($value) &lt; 2) {
			$this-&gt;_error(self::MISSING);
    	}

    	$this-&gt;_field1 = array_shift($value);
    	$this-&gt;_field2 = array_shift($value);

        if($this-&gt;_case === true) $function = 'strcmp';
        else $function = 'strcasecmp';

        if(0 !== $function($this-&gt;_field1,$this-&gt;_field2)) $this-&gt;_error(self::NOT_EQUAL);

        if (count($this-&gt;_messages)) {
            return false;
        } else {
            return true;
        }
    }
}
?&gt;</pre>
<p>Here is a sample test case. Validate password and confirm password elements represented by &#8216;password&#8217; and &#8216;cpassword&#8217; element names respectively.</p>
<pre class="brush: php">$filters = array('password' =&gt; 'StringTrim', 'cpassword' =&gt; 'StringTrim');
$validators = array(
    'Password' =&gt; array(
        'presence' =&gt; 'required',
        array('StringLength',5,15),
        'fields' =&gt; 'password',
        'messages' =&gt; "Passwords must be between 5 and 15 characters in length."),
    'Confirm password' =&gt; array(
        array('StringEquals'),
        'fields' =&gt; array('password','cpassword'),
        'messages' =&gt; array(
            0 =&gt; array(
                Zend_Validate_StringEquals::NOT_EQUAL =&gt; "Passwords does not match.",
                Zend_Validate_StringEquals::MISSING =&gt; "Both password fields must be filled."))));

$inputdata = new Zend_Filter_Input($filter,$validators,$_POST,$options);</pre>
]]></content:encoded>
			<wfw:commentRss>http://dotmanila.com/blog/2010/01/zend_validate_stringequals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

