intro
This article illustrates how to create a custom validator to compare values of elements of Zend_Form. You can download the source code of this article from here.
1.Create Validator
The validator of the following specifications is made.
parameter | description |
---|---|
$key | String value to identify which fields are compared. |
$operator |
Operator to compare the fields. The operator is not specified in the element in the comparison destination. |
Error Message Key | Initial Error Message |
---|---|
fieldNotSame | ‘%value%’ is not equal to ‘%key_value%’ |
fieldNotGreaterThan | ‘%value%’ is not greater than ‘%key_value%’ |
fieldNotGreaterThanEqual | ‘%value%’ is not greater than or equal to ‘%key_value%’ |
fieldNotLessThan | ‘%value%’ is not less than ‘%key_value%’ |
fieldNotLessThanEqual | ‘%value%’ is not less than or equal to ‘%key_value%’ |
Create the file library/My/Validate/FieldCompare.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
<span style="color: #000000; font-weight: bold;"><?php</span> <span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'Zend/Validate/Abstract.php'</span>; <span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'Zend/Validate/Hostname.php'</span>; <span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'Zend/Validate/Exception.php'</span>; <span style="color: #000000; font-weight: bold;">class</span> My_Validate_FieldCompare <span style="color: #000000; font-weight: bold;">extends</span> Zend_Validate_Abstract <span style="color: #66cc66;">{</span> const MSG_NOT_EQ = <span style="color: #ff0000;">'fieldNotSame'</span>; const MSG_NOT_GT = <span style="color: #ff0000;">'fieldNotGreaterThan'</span>; const MSG_NOT_GE = <span style="color: #ff0000;">'fieldNotGreaterThanEqual'</span>; const MSG_NOT_LT = <span style="color: #ff0000;">'fieldNotLessThan'</span>; const MSG_NOT_LE = <span style="color: #ff0000;">'fieldNotLessThanEqual'</span>; const EQ = <span style="color: #ff0000;">'eq'</span>; const GT = <span style="color: #ff0000;">'gt'</span>; const GE = <span style="color: #ff0000;">'ge'</span>; const LT = <span style="color: #ff0000;">'lt'</span>; const LE = <span style="color: #ff0000;">'le'</span>; protected <span style="color: #0000ff;">$_messageVariables</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span> <span style="color: #ff0000;">'key'</span> => <span style="color: #ff0000;">'_key'</span>, <span style="color: #ff0000;">'key_value'</span> => <span style="color: #ff0000;">'_key_value'</span> <span style="color: #66cc66;">)</span>; protected <span style="color: #0000ff;">$_messageTemplates</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span> self::<span style="color: #006600;">MSG_NOT_EQ</span> => <span style="color: #ff0000;">"'%value%' is not equal to '%key_value%'"</span>, self::<span style="color: #006600;">MSG_NOT_GT</span> => <span style="color: #ff0000;">"'%value%' is not greater than '%key_value%'"</span>, self::<span style="color: #006600;">MSG_NOT_GE</span> => <span style="color: #ff0000;">"'%value%' is not greater than or equal to '%key_value%'"</span>, self::<span style="color: #006600;">MSG_NOT_LT</span> => <span style="color: #ff0000;">"'%value%' is not less than '%key_value%'"</span>, self::<span style="color: #006600;">MSG_NOT_LE</span> => <span style="color: #ff0000;">"'%value%' is not less than or equal to '%key_value%'"</span>, <span style="color: #66cc66;">)</span>; protected <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$_tmp_operators</span> = <span style="color: #000000; font-weight: bold;">null</span>; protected <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$_tmp_values</span> = <span style="color: #000000; font-weight: bold;">null</span>; protected <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$_key_errors</span> = <span style="color: #000000; font-weight: bold;">null</span>; protected <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$_key_messages</span> = <span style="color: #000000; font-weight: bold;">null</span>; protected <span style="color: #0000ff;">$_key</span> = <span style="color: #000000; font-weight: bold;">null</span>; protected <span style="color: #0000ff;">$_operator</span> = <span style="color: #000000; font-weight: bold;">null</span>; protected <span style="color: #0000ff;">$_key_value</span> = <span style="color: #000000; font-weight: bold;">null</span>; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$key</span>, <span style="color: #0000ff;">$operator</span> = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #0000ff;">$this</span>->_key = <span style="color: #0000ff;">$key</span>; <span style="color: #0000ff;">$this</span>->_operator = <span style="color: #0000ff;">$operator</span>; self::<span style="color: #0000ff;">$_key_errors</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">]</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>; self::<span style="color: #0000ff;">$_key_messages</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">]</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>; <span style="color: #66cc66;">}</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isValid<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$this</span>->_operator && <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span>self::<span style="color: #0000ff;">$_tmp_values</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #0000ff;">$this</span>->_key_value = self::<span style="color: #0000ff;">$_tmp_values</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span>; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$this</span>->_operator && <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span>self::<span style="color: #0000ff;">$_tmp_operators</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #0000ff;">$this</span>->_key_value = <span style="color: #0000ff;">$value</span>; <span style="color: #0000ff;">$this</span>->_operator = self::<span style="color: #0000ff;">$_tmp_operators</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span>; <span style="color: #0000ff;">$value</span> = self::<span style="color: #0000ff;">$_tmp_values</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span>; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$this</span>->_operator && !<a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span>self::<span style="color: #0000ff;">$_tmp_values</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> self::<span style="color: #0000ff;">$_tmp_operators</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span> = <span style="color: #0000ff;">$this</span>->_operator; self::<span style="color: #0000ff;">$_tmp_values</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span> = <span style="color: #0000ff;">$value</span>; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$this</span>->_operator && <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span>self::<span style="color: #0000ff;">$_tmp_values</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> throw <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_Exception<span style="color: #66cc66;">(</span> <span style="color: #ff0000;">'Field Compare Validator : operator does not specified(key='</span> . <span style="color: #0000ff;">$this</span>->_key . <span style="color: #ff0000;">')'</span><span style="color: #66cc66;">)</span>; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$this</span>->_operator && !<a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span>self::<span style="color: #0000ff;">$_tmp_operators</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> self::<span style="color: #0000ff;">$_tmp_values</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span> = <span style="color: #0000ff;">$value</span>; <span style="color: #66cc66;">}</span> <span style="color: #0000ff;">$this</span>->_setValue<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span><span style="color: #66cc66;">)</span>; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$this</span>->_key_value && <span style="color: #0000ff;">$this</span>->_operator<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$this</span>->_operator<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">case</span> self::<span style="color: #006600;">EQ</span> : <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span> == <span style="color: #0000ff;">$this</span>->_key_value<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #66cc66;">}</span> <span style="color: #0000ff;">$this</span>->_error<span style="color: #66cc66;">(</span>self::<span style="color: #006600;">MSG_NOT_EQ</span><span style="color: #66cc66;">)</span>; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #b1b100;">case</span> self::<span style="color: #006600;">GT</span> : <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span> > <span style="color: #0000ff;">$this</span>->_key_value<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #66cc66;">}</span> <span style="color: #0000ff;">$this</span>->_error<span style="color: #66cc66;">(</span>self::<span style="color: #006600;">MSG_NOT_GT</span><span style="color: #66cc66;">)</span>; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #b1b100;">case</span> self::<span style="color: #006600;">GE</span> : <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span> >= <span style="color: #0000ff;">$this</span>->_key_value<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #66cc66;">}</span> <span style="color: #0000ff;">$this</span>->_error<span style="color: #66cc66;">(</span>self::<span style="color: #006600;">MSG_NOT_GE</span><span style="color: #66cc66;">)</span>; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #b1b100;">case</span> self::<span style="color: #006600;">LT</span> : <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span> < <span style="color: #0000ff;">$this</span>->_key_value<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #66cc66;">}</span> <span style="color: #0000ff;">$this</span>->_error<span style="color: #66cc66;">(</span>self::<span style="color: #006600;">MSG_NOT_LT</span><span style="color: #66cc66;">)</span>; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #b1b100;">case</span> self::<span style="color: #006600;">LE</span> : <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span> <= <span style="color: #0000ff;">$this</span>->_key_value<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #66cc66;">}</span> <span style="color: #0000ff;">$this</span>->_error<span style="color: #66cc66;">(</span>self::<span style="color: #006600;">MSG_NOT_LE</span><span style="color: #66cc66;">)</span>; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #66cc66;">}</span> throw <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_Exception<span style="color: #66cc66;">(</span> <span style="color: #ff0000;">'Field Compare Validator : operator not found(key='</span> . <span style="color: #0000ff;">$this</span>->_key . <span style="color: #ff0000;">',op='</span> . <span style="color: #0000ff;">$this</span>->_operator . <span style="color: #ff0000;">')'</span><span style="color: #66cc66;">)</span>; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #66cc66;">}</span> <span style="color: #808080; font-style: italic;">/** * Sets the value to be validated and clears the messages and errors arrays * * @param mixed $value * @return void */</span> protected <span style="color: #000000; font-weight: bold;">function</span> _setValue<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> parent::_setValue<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span><span style="color: #66cc66;">)</span>; self::<span style="color: #0000ff;">$_key_errors</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>; self::<span style="color: #0000ff;">$_key_messages</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>; <span style="color: #66cc66;">}</span> <span style="color: #808080; font-style: italic;">/** * @param string $messageKey OPTIONAL * @param string $value OPTIONAL * @return void */</span> protected <span style="color: #000000; font-weight: bold;">function</span> _error<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$messageKey</span> = <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #0000ff;">$value</span> = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$messageKey</span> === <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #0000ff;">$keys</span> = <a href="http://www.php.net/array_keys"><span style="color: #000066;">array_keys</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$this</span>->_messageTemplates<span style="color: #66cc66;">)</span>; <span style="color: #0000ff;">$messageKey</span> = <a href="http://www.php.net/current"><span style="color: #000066;">current</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$keys</span><span style="color: #66cc66;">)</span>; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$value</span> === <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #0000ff;">$value</span> = <span style="color: #0000ff;">$this</span>->_value; <span style="color: #66cc66;">}</span> self::<span style="color: #0000ff;">$_key_errors</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span><span style="color: #66cc66;">[</span><span style="color: #66cc66;">]</span> = <span style="color: #0000ff;">$messageKey</span>; self::<span style="color: #0000ff;">$_key_messages</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$messageKey</span><span style="color: #66cc66;">]</span> = <span style="color: #0000ff;">$this</span>->_createMessage<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$messageKey</span>, <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">)</span>; <span style="color: #66cc66;">}</span> <span style="color: #808080; font-style: italic;">/** * Returns array of validation failure messages * * @return array */</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMessages<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$this</span>->_operator<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">return</span> self::<span style="color: #0000ff;">$_key_messages</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span>; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>; <span style="color: #66cc66;">}</span> <span style="color: #808080; font-style: italic;">/** * Returns array of validation failure message codes * * @return array * @deprecated Since 1.5.0 */</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getErrors<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$this</span>->_operator<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #b1b100;">return</span> self::<span style="color: #0000ff;">$_key_errors</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$this</span>->_key<span style="color: #66cc66;">]</span>; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>; <span style="color: #66cc66;">}</span> <span style="color: #66cc66;">}</span> |
2.Add Validator
Add the validator like the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$form = new Zend_Form(); $form->addElementPrefixPath('My_Validate', 'My/Validate/', Zend_Form_Element::VALIDATE); //... $email = $form->createElement('text', 'email'); $email->setLabel('E-Mail') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('emailAddress', false) ->addValidator('fieldCompare', false, array('email')); $email2 = $form->createElement('text', 'email2'); $email2->setLabel('E-Mail(Confirm)') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('emailAddress', false) ->addValidator('fieldCompare', false, array('email', 'eq')); //... |
This will execute the comparison $email == $email2
.
The following is another example.
1 2 3 4 5 6 7 8 9 |
$min = $form->createElement('text', 'min'); $min->setLabel('Min Value') ->setRequired(true) ->addValidator('fieldCompare', false, array('min')); $value = $form->createElement('text', 'value'); $value->setLabel('E-Mail(Confirm)') ->setRequired(true) ->addValidator('fieldCompare', false, array('min', 'ge')); //... |
This will execute the comparison $value >= $min
.
Thanks, good one.
There is only one problem: if I use it for passwords, the %key_value% is being replaced by a visible password text, not *****.
I added one more operation EQPSW and message MSG_NOT_EQPSW which does not show %key_value%.
In complementary answer to midix, I set a certain echo mode, that replace the %key_value% instead of don’t show it.
Otherwise, continue your good works.
There could be a problem when it comes to unit testing. So try to reset $_tmp_values while class construction.
Here is the cleaned constructor:
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
public function __construct($key, $operator = null)
{
$this->_key = $key;
$this->_operator = $operator;
self::$_key_errors[$key] = array();
self::$_key_messages[$key] = array();
self::$_tmp_values = null;
}
Now your testing problems should be resolved.
// create class instance counter
protected static $_instances_created = 0;
// reset input list when two values were compared
public static function resetInputList()
{
if(self::$_instances_created == 2)
{
self::$_tmp_values = null;
self::$_instances_created = 0;
return true;
}
self::$_instances_created = self::$_instances_created + 1;
}
// construct and reset the compare list
public function __construct($key, $operator = null)
{
self::resetInputList();
$this->_key = $key;
$this->_operator = $operator;
self::$_key_errors[$key] = array();
self::$_key_messages[$key] = array();
self::$_tmp_operators = null;
}
Sorry, my first post was wrong. Please refer to my second post where I only reset the list if a second instance of the class was created.
Wow, Thank you very Much!!
Thank you very much!!