intro
Zend_Form用のフィールド比較バリデータを作成します。ソースコードはこちらからダウンロードできます。
1.バリデーションクラスの作成
以下のような仕様のバリデータを作成します。
パラメータ | 説明 |
---|---|
$key |
フィールドを比較する際の識別子。 同じキーを持つフィールド同士が比較される。 |
$operator |
フィールドを比較する際の演算子。 比較先のフィールドでは演算子を指定しない。 |
エラーメッセージキー | 初期エラーメッセージ |
---|---|
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%’ |
以下のようにlibrary/My/Validate/FieldCompare.php
ファイルを作成します。
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.バリデータの追加
以下のようにフォームにバリデータを追加します。
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')); //... |
上記コードでは$email == $email2
の比較が行われます。
以下はもうひとつの例です。
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')); //... |
上記コードでは$value >= $min
の比較が行われます。