Created
March 2, 2014 09:46
-
-
Save matstani/9304210 to your computer and use it in GitHub Desktop.
ZF2 バリデータの例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 半角数字のバリデータ | |
* | |
* @return array バリデータのspec | |
*/ | |
public static function digitsValidator() | |
{ | |
return array( | |
'name' =>'Digits', | |
'break_chain_on_failure' => true, | |
'options' => array( | |
'messages' => array( | |
\Zend\Validator\Digits::NOT_DIGITS => '半角数字のみを入力して下さい', | |
) | |
) | |
); | |
} | |
/** | |
* 範囲のバリデータ | |
* | |
* @param string $fromField 範囲開始フィールド名 | |
* @param string $toField 範囲終了フィールド名 | |
* @return array バリデータのspec | |
*/ | |
public static function rangeValidator($fromField, $toField) | |
{ | |
return array( | |
'name' => 'Callback', | |
'break_chain_on_failure' => true, | |
'options' => array( | |
'messages' => array( | |
\Zend\Validator\Callback::INVALID_VALUE => '範囲を正しく指定して下さい', | |
), | |
'callback' => function($value, $context=array()) use ($fromField, $toField) { | |
$from = $context[$fromField]; | |
$to = $context[$toField]; | |
if ($from && $to) { | |
return $from <= $to; | |
} else { | |
return true; | |
} | |
}, | |
), | |
); | |
} | |
/** | |
* 時刻のバリデータ | |
* | |
* @return array バリデータのspec | |
*/ | |
public static function timeValidator() | |
{ | |
return array( | |
'name' => 'Regex', | |
'break_chain_on_failure' => true, | |
'options' => array( | |
'pattern' => '/^(([0-1]+[0-9])|(2[0-3])):([0-5]+[0-9])$/', | |
'messages' => array( | |
\Zend\Validator\Regex::NOT_MATCH => '時刻を「HH:MM」の形式で入力して下さい' | |
) | |
) | |
); | |
} | |
/** | |
* 全角カナのバリデータ | |
* | |
* @return array バリデータのspec | |
*/ | |
public static function zenKanaValidator() | |
{ | |
return array( | |
'name' => 'Regex', | |
'break_chain_on_failure' => true, | |
'options' => array( | |
'pattern' => '/^[ァ-ヾ]+$/u', | |
'messages' => array( | |
\Zend\Validator\Regex::NOT_MATCH => '全角カナを入力して下さい' | |
) | |
) | |
); | |
} | |
/** | |
* 日付のバリデータ | |
* | |
* @return array バリデータのspec | |
*/ | |
public static function dateValidator() | |
{ | |
$msg = '日付を「yyyy/mm/dd」の形式で入力して下さい'; | |
return array( | |
'name' => 'Date', | |
'break_chain_on_failure' => true, | |
'options' => array( | |
'format' => 'Y/m/d', | |
'messages' => array( | |
\Zend\Validator\Date::INVALID => $msg, | |
\Zend\Validator\Date::INVALID_DATE => $msg, | |
\Zend\Validator\Date::FALSEFORMAT => $msg, | |
) | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment