/
add_validation_rule.php
391 lines (344 loc) · 11.8 KB
/
add_validation_rule.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
* 独自のバリデーションルールを追加するbehavior プラグイン
* 内部文字コードはデフォルトUTF-8(オプションで変更可能)
* Behavior of some validation rules.
* Internal encoding is UTF-8, can change it with parameter.
*
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2009, Yasushi Ichikawa. (http://d.hatena.ne.jp/cakephper/)
* @link http://d.hatena.ne.jp/cakephper/
* @package cakeplus
* @subpackage add_validation_rule
* @version 0.04
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*
*
* =====利用方法=====
* 各モデルファイルで、下記のように使う。app_modelにactsAsで指定しても可
* In each model class or app_model, write code as follow.
* var $actsAs = array('Cakeplus.AddValidationRule');
*
* 内部文字コードを変更したい場合は、オプションで変更可能(デフォルトはUTF-8)
* If you want to change encoding(UTF-8), write code as follow.
* var $actsAs = array('Cakeplus.AddValidationRule' => array('encoding' => 'EUC') );
*
*
* 各モデルファイル内のバリデーションの書き方は下記を参考に。
* Example: validation definition in a model.
* var $validate = array(
* 'test' => array(
* "rule2" => array('rule' => array('maxLengthJP', 5),
* 'message' => '5文字以内です'
* ),
* "rule3" => array('rule' => array('minLengthJP', 2),
* 'message' => '2文字以上です'
* ),
* "rule4" => array('rule' => array('compare2fields', 'test_conf'),
* 'message' => '値が違います'
* ),
* "rule5" => array('rule' => array('spaceOnly'),
* 'message' => 'スペース以外も入力してください'
* ),
* "rule6" => array('rule' => array('katakanaOnly'),
* 'message' => 'カタカナのみ入力してください'
* ),
* "rule7" => array('rule' => array('betweenJP', 5, 10),
* 'message' => '5文字以上、10文字以内です'
* ),
* "rule8" => array('rule' => array('hiraganaOnly'),
* 'message' => 'ひらがなのみ入力してください'
* ),
* "rule9" => array('rule' => array('zenkakuOnly'),
* 'message' => '全角文字のみ入力してください'
* ),
* "rule10" => array('rule' => array('datetime'),
* 'message' => '正しい日時を入力してください'
* ),
* ),
* );
*
* Authコンポーネントでパスワードフィールドがハッシュ化されている場合は、compare2fieldsの第3配列にtrueを指定する
* Using Auth component, If you want compare password and password confirm field,
* set "true" in 3rd parameter of compare2fields validation, password_conf field is encrypted.
* var $validate = array(
* 'password' => array(
* "rule" => array('rule' => array('compare2fields', 'password_conf',true),
* 'message' => '値が違います'
* ),
* ),
* );
*
*
*/
class AddValidationRuleBehavior extends ModelBehavior {
function setup(&$model, $config = array()){
//change encoding with parameter option.
if( !empty( $config['encoding'] ) ){
mb_internal_encoding($config['encoding']);
}else{
mb_internal_encoding("UTF-8");
}
}
/**
* マルチバイト用バリデーション 文字数上限チェック
* check max length with Multibyte character.
*
* @param array &$model model object, automatically set
* @param array $wordvalue field value, automatically set
* @param int $length max length number
* @return boolean
*/
function maxLengthJP( &$model, $wordvalue, $length ) {
$word = array_shift($wordvalue);
return( mb_strlen( $word ) <= $length );
}
/**
* マルチバイト用バリデーション 文字数下限チェック
* check min length with Multibyte character.
*
* @param array &$model model object, automatically set
* @param array $wordvalue field value, automatically set
* @param int $length min length number
* @return boolean
*/
function minLengthJP( &$model, $wordvalue, $length ) {
$word = array_shift($wordvalue);
return( mb_strlen( $word ) >= $length );
}
/**
* マルチバイト用のbetweenバリデーション
*
*
* @param array &$model
* @param array $wordvalue
* @param int $low
* @param int $high
* @return boolean
*/
function betweenJP(&$model, $wordvalue, $low, $high) {
$value = array_shift($wordvalue);
if ( mb_strlen($value) >= $low && mb_strlen($value) <= $high ) {
return true;
} else {
return false;
}
}
/**
* フィールド値の比較
* emailとemail_confフィールドを比較する場合などに利用
* $compare_filedに比較したいフィールド名をセットする(必須)
* Compare 2 fields value. Example, email field and email_conf field.
* Set field name for comparison in $compare_filed
*
* authにtrueを指定すると、Authコンポーネントのパスワードフィールドを前提として
* 比較するpassword_confフィールドの値をハッシュ化する
* If set "true" in $auth, $compare_filed is encrypted with Security::hash.
*
* @param array &$model model object, automatically set
* @param array $wordvalue field value, automatically set
* @param string $compare_filed set field name for comparison
* @param boolean $auth set true, $compare_filed is encrypted with Security::hash
* @return boolean
*/
function compare2fields( &$model, $wordvalue , $compare_field , $auth = false ){
$field = current($wordvalue);
$compare = isset($model->data[$model->alias][$compare_field]) ? $model->data[$model->alias][$compare_field] : null;
if( $auth === true ){
App::import('Component','Auth');
return $field === AuthComponent::password($compare);
} else {
return $field === $compare;
}
}
/**
* 全角ひらがな以外が含まれていればエラーとするバリデーションチェック
* 全角ダッシュ「ー」のみ必要と考えられるので追加
* Japanese HIRAGANA Validation
* @param array &$model
* @param array $wordvalue
* @return boolean
*/
function hiraganaOnly( &$model, $wordvalue){
$value = array_shift($wordvalue);
return preg_match("/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc))*$/", $value);
}
/**
* 全角ひらがな以外が含まれていればエラーとするバリデーションチェック
* Japanese HIRAGANA Validation
* (old name, keep this for backward compatibility)
*/
function hiragana_only( &$model, $wordvalue){
return $this->hiraganaOnly($model, $wordvalue);
}
/**
* 全角カタカナ以外が含まれていればエラーとするバリデーションチェック
* Japanese KATAKANA Validation
*
* @param array &$model
* @param array $wordvalue
* @return boolean
*/
function katakanaOnly( &$model, $wordvalue){
$value = array_shift($wordvalue);
//\xe3\x82\x9b 濁点゛
//\xe3\x82\x9c 半濁点゜
return preg_match("/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c))*$/", $value);
}
/**
* 全角カタカナ以外が含まれていればエラーとするバリデーションチェック
* Japanese KATAKANA Validation
* (old name, keep this for backward compatibility)
*/
function katakana_only( &$model, $wordvalue){
return $this->katakanaOnly($model, $wordvalue);
}
/**
* マルチバイト文字以外が含まれていればエラーとするバリデーションチェック
* Japanese ZENKAKU Validation
*
* @param array &$model
* @param array $wordvalue
* @return boolean
*/
function zenkakuOnly( &$model, $wordvalue){
$value = array_shift($wordvalue);
return !preg_match("/(?:\xEF\xBD[\xA1-\xBF]|\xEF\xBE[\x80-\x9F])|[\x20-\x7E]/", $value);
}
/**
* マルチバイト文字以外が含まれていればエラーとするバリデーションチェック
* Japanese ZENKAKU Validation
* (old name, keep this for backward compatibility)
*/
function zenkaku_only( &$model, $wordvalue){
return $this->zenkakuOnly($model, $wordvalue);
}
/**
* 全角、半角スペースのみであればエラーとするバリデーションチェック
* Japanese Space only validation
*
* @param array &$model
* @param array $wordvalue
* @return boolean
*/
function spaceOnly( &$model, $wordvalue){
$value = array_shift($wordvalue);
if( mb_ereg_match("^(\s| )+$", $value) ){
return false;
}else{
return true;
}
}
/**
* 全角、半角スペースのみであればエラーとするバリデーションチェック
* Japanese Space only validation
* (old name, keep this for backward compatibility)
*
*/
function space_only( &$model, $wordvalue){
return $this->spaceOnly($model, $wordvalue);
}
/**
* only Allow 0-9, a-z , A-Z
* check it including Multibyte characters.
*
* @param array ref &$model
* @param array $wordvalue
* @return boolean
*/
function alphaNumber( &$model, $wordvalue ){
$value = array_shift($wordvalue);
return preg_match( "/^[a-zA-Z0-9]*$/", $value );
}
/**
* only Allow 0-9, a-z , A-Z
* check it including Multibyte characters.
* (old name, keep this for backward compatibility)
*
*/
function alpha_number( &$model, $wordvalue ){
return $this->alphaNumber($model, $wordvalue);
}
/**
* Japan Telephone and Fax validation
*
*/
function telFaxJp(&$model, $wordvalue) {
$value = array_shift($wordvalue);
$pattern = '/^(0\d{1,4}[\s-]?\d{1,4}[\s-]?\d{1,4}|\+\d{1,3}[\s-]?\d{1,4}[\s-]?\d{1,4}[\s-]?\d{1,4})$/';
return preg_match( $pattern, $value );
}
/**
* Japan Telephone and Fax validation
* (old name, keep this for backward compatibility)
*
*/
function tel_fax_jp(&$model, $wordvalue) {
return $this->telFaxJp($model, $wordvalue);
}
/**
* Mobile Email validation
*
*/
function mobileEmailJp(&$model, $wordvalue) {
$value = array_shift($wordvalue);
$pattern = '/^[a-z0-9\._-]{3,30}@(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4})$/i';
return preg_match( $pattern, $value );
}
/**
* Mobile Email validation
* (old name, keep this for backward compatibility)
*
*/
function mobile_email_jp(&$model, $wordvalue) {
return $this->mobileEmailJp($model, $wordvalue);
}
/**
* password validation
* Only AlphaNumeric , check letter length
*/
function passwordValid( &$model, $wordvalue , $compare_filed , $min=5, $max=15 ){
$pass_val = $model->data[$model->alias][ $compare_filed ];
$pattern = '/^[a-zA-Z0-9]{'. $min .','. $max .'}$/';
return preg_match($pattern, $pass_val);
}
/**
* password validation
* (old name, keep this for backward compatibility)
*/
function password_valid( &$model, $wordvalue , $compare_filed , $min=5, $max=15 ){
return $this->passwordValid($model, $wordvalue , $compare_filed , $min, $max);
}
/**
* Datetime validation, determines if the string passed is a valid datetime.
* Using self date and time validation methods.
*
* @param string $check a valid datetime string
* @param mixed $format Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc)
* @param string $regex If a custom regular expression is used this is the only validation that will occur.
* @return boolean Success
* @access public
*/
function datetime(&$model, $wordvalue, $format = 'ymd', $regex = null) {
$_this =& Validation::getInstance();
$_this->__reset();
$value = array_shift($wordvalue);
$pattern = '%^(.+) (\d+:\d+[APap][Mm])$|^(.+) (\d+:\d+)$%';
preg_match($pattern, $value, $match);
if(!empty($match[1]) && !empty($match[2])) {
$date = $match[1];
$time = $match[2];
} else if(!empty($match[3]) && !empty($match[4])) {
$date = $match[3];
$time = $match[4];
}
if(empty($date) || empty($time)){
return false;
}
return $_this->date($date, $format, $regex) && $_this->time($time);
}
}
?>