Programmer's Reference Guide
| バリデータの書き方 |
検証メッセージ
Zend_Validate を継承したバリデータには、 検証に失敗したときに使用するメッセージが用意されています。 ここにまとめた情報をもとに、 独自のメッセージを設定したりバリデータが返すメッセージを翻訳したりできます。 次の表は、各バリデータが返すすべてのメッセージをまとめたものです。
| バリデータ | 定数 | メッセージ |
|---|---|---|
| Alnum | NOT_ALNUM | '%value%' has not only alphabetic and digit characters |
| STRING_EMPTY | '%value%' is an empty string | |
| Alpha | NOT_ALPHA | '%value%' has not only alphabetic characters |
| STRING_EMPTY | '%value%' is an empty string | |
| Barcode | --- | messages are thrown by a subclass |
| Barcode_Ean13 | INVALID | '%value%' is an invalid EAN-13 barcode |
| INVALID_LENGTH | '%value%' should be 13 characters | |
| NOT_NUMERIC | '%value%' should contain only numeric characters | |
| Barcode_UpcA | INVALID | '%value%' is an invalid UPC-A barcode |
| INVALID_LENGTH | '%value%' should be 12 characters | |
| Between | NOT_BETWEEN | '%value%' is not between '%min%' and '%max%', inclusively |
| NOT_BETWEEN_STRICT | '%value%' is not strictly between '%min%' and '%max%' | |
| Ccnum | LENGTH | '%value%' must contain between 13 and 19 digits |
| CHECKSUM | Luhn algorithm (mod-10 checksum) failed on '%value%' | |
| Date | FALSEFORMAT | '%value%' does not fit given date format |
| INVALID | '%value%' does not appear to be a valid date | |
| Db_Abstract | ERROR_NO_RECORD_FOUND | No record matching %value% was found |
| ERROR_RECORD_FOUND | A record matching %value% was found | |
| Digits | NOT_DIGITS | '%value%' contains not only digit characters |
| STRING_EMPTY | '%value%' is an empty string | |
| EmailAddress | INVALID | '%value%' is not a valid email address in the basic format local-part@hostname |
| INVALID_FORMAT | '%value%' is not a valid email address in the basic format local-part@hostname | |
| INVALID_HOSTNAME | '%hostname%' is not a valid hostname for email address '%value%' | |
| INVALID_MX_RECORD | '%hostname%' does not appear to have a valid MX record for the email address '%value%' | |
| DOT_ATOM | '%localPart%' not matched against dot-atom format | |
| QUOTED_STRING | '%localPart%' not matched against quoted-string format | |
| INVALID_LOCAL_PART | '%localPart%' is not a valid local part for email address '%value%' | |
| LENGTH_EXCEEDED | '%value%' exceeds the allowed length | |
| File_Count | TOO_MUCH | Too much files, maximum '%max%' are allowed but '%count%' are given |
| TOO_LESS | Too less files, minimum '%min%' are expected but '%count%' are given | |
| File_Crc32 | DOES_NOT_MATCH | The file '%value%' does not match the given crc32 hashes |
| NOT_DETECTED | There was no crc32 hash detected for the given file | |
| NOT_FOUND | The file '%value%' could not be found | |
| File_ExcludeExtension | FALSE_EXTENSION | The file '%value%' has a false extension |
| NOT_FOUND | The file '%value%' was not found | |
| File_ExcludeMimeType | FALSE_TYPE | The file '%value%' has a false mimetype of '%type%' |
| NOT_DETECTED | The mimetype of file '%value%' could not been detected | |
| NOT_READABLE | The file '%value%' can not be read | |
| File_Exists | DOES_NOT_EXIST | The file '%value%' does not exist |
| File_Extension | FALSE_EXTENSION | The file '%value%' has a false extension |
| NOT_FOUND | The file '%value%' was not found | |
| File_FilesSize | TOO_BIG | All files in sum should have a maximum size of '%max%' but '%size%' were detected |
| TOO_SMALL | All files in sum should have a minimum size of '%min%' but '%size%' were detected | |
| NOT_READABLE | One or more files can not be read | |
| File_Hash | DOES_NOT_MATCH | The file '%value%' does not match the given hashes |
| NOT_DETECTED | There was no hash detected for the given file | |
| NOT_FOUND | The file '%value%' could not be found | |
| File_ImageSize | WIDTH_TOO_BIG | Maximum allowed width for image '%value%' should be '%maxwidth%' but '%width%' detected |
| WIDTH_TOO_SMALL | Minimum expected width for image '%value%' should be '%minwidth%' but '%width%' detected | |
| HEIGHT_TOO_BIG | Maximum allowed height for image '%value%' should be '%maxheight%' but '%height%' detected | |
| HEIGHT_TOO_SMALL | Minimum expected height for image '%value%' should be '%minheight%' but '%height%' detected | |
| NOT_DETECTED | The size of image '%value%' could not be detected | |
| NOT_READABLE | The image '%value%' can not be read | |
| File_IsCompressed | FALSE_TYPE | The file '%value%' is not compressed, '%type%' detected |
| NOT_DETECTED | The mimetype of file '%value%' could not been detected | |
| NOT_READABLE | The file '%value%' can not be read | |
| File_IsImage | FALSE_TYPE | The file '%value%' is no image, '%type%' detected |
| NOT_DETECTED | The mimetype of file '%value%' could not been detected | |
| NOT_READABLE | The file '%value%' can not be read | |
| File_Md5 | DOES_NOT_MATCH | The file '%value%' does not match the given md5 hashes |
| NOT_DETECTED | There was no md5 hash detected for the given file | |
| NOT_FOUND | The file '%value%' could not be found | |
| File_MimeType | FALSE_TYPE | The file '%value%' has a false mimetype of '%type%' |
| NOT_DETECTED | The mimetype of file '%value%' could not been detected | |
| NOT_READABLE | The file '%value%' can not be read | |
| File_NotExists | DOES_EXIST | The file '%value%' does exist |
| File_Sha1 | DOES_NOT_MATCH | The file '%value%' does not match the given sha1 hashes |
| NOT_DETECTED | There was no sha1 hash detected for the given file | |
| NOT_FOUND | The file '%value%' could not be found | |
| File_Size | TOO_BIG | Maximum allowed size for file '%value%' is '%max%' but '%size%' detected |
| TOO_SMALL | Minimum expected size for file '%value%' is '%min%' but '%size%' detected | |
| NOT_FOUND | The file '%value%' could not be found | |
| File_Upload | INI_SIZE | The file '%value%' exceeds the defined ini size |
| FORM_SIZE | The file '%value%' exceeds the defined form size | |
| PARTIAL | The file '%value%' was only partially uploaded | |
| NO_FILE | The file '%value%' was not uploaded | |
| NO_TMP_DIR | No temporary directory was found for the file '%value%' | |
| CANT_WRITE | The file '%value%' can't be written | |
| EXTENSION | The extension returned an error while uploading the file '%value%' | |
| ATTACK | The file '%value%' was illegal uploaded, possible attack | |
| FILE_NOT_FOUND | The file '%value%' was not found | |
| UNKNOWN | Unknown error while uploading the file '%value%' | |
| File_WordCount | TOO_MUCH | Too much words, maximum '%max%' are allowed but '%count%' were counted |
| TOO_LESS | Too less words, minimum '%min%' are expected but '%count%' were counted | |
| NOT_FOUND | The file '%value%' could not be found | |
| Float | NOT_FLOAT | '%value%' does not appear to be a float |
| GreaterThan | NOT_GREATER | '%value%' is not greater than '%min%' |
| Hex | NOT_HEX | '%value%' has not only hexadecimal digit characters |
| Hostname | IP_ADDRESS_NOT_ALLOWED | '%value%' appears to be an IP address, but IP addresses are not allowed |
| UNKNOWN_TLD | '%value%' appears to be a DNS hostname but cannot match TLD against known list | |
| INVALID_DASH | '%value%' appears to be a DNS hostname but contains a dash (-) in an invalid position | |
| INVALID_HOSTNAME_SCHEMA | '%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%' | |
| UNDECIPHERABLE_TLD | '%value%' appears to be a DNS hostname but cannot extract TLD part | |
| INVALID_HOSTNAME | '%value%' does not match the expected structure for a DNS hostname | |
| INVALID_LOCAL_NAME | '%value%' does not appear to be a valid local network name | |
| LOCAL_NAME_NOT_ALLOWED | '%value%' appears to be a local network name but local network names are not allowed | |
| Iban | NOTSUPPORTED | '%value%' does not have IBAN |
| FALSEFORMAT | '%value%' has a false format | |
| CHECKFAILED | '%value%' has failed the IBAN check | |
| Identical | NOT_SAME | The token '%token%' does not match the given token '%value%' |
| MISSING_TOKEN | No token was provided to match against | |
| InArray | NOT_IN_ARRAY | '%value%' was not found in the haystack |
| Int | NOT_INT | '%value%' does not appear to be an integer |
| Ip | NOT_IP_ADDRESS | '%value%' does not appear to be a valid IP address |
| LessThan | NOT_LESS | '%value%' is not less than '%max%' |
| NotEmpty | IS_EMPTY | Value is required and can't be empty |
| Regex | NOT_MATCH | '%value%' does not match against pattern '%pattern%' |
| StringLength | TOO_SHORT | '%value%' is less than %min% characters long |
| TOO_LONG | '%value%' is greater than %max% characters long |
さらに、 getMessageTemplates() メソッドを使うとバリデータの全メッセージテンプレートを取得できます。 これは、検証に失敗したときにバリデータが返す可能性のあるメッセージを配列で返します。
- $validator = new Zend_Validate_Alnum();
- $messages = $validator->getMessageTemplates();
検証メッセージのサイズの制限
検証メッセージの最大サイズを制限しなければならないこともあるでしょう。 たとえば、1 行に 100 文字までしかレンダリングできないなどの制限がビューにある場合です。 このような場合のため、Zend_Validate では自動的に検証メッセージの最大長を制限できるようになっています。
実際に設定されているサイズを取得するには Zend_Validate::getMessageLength() を使用します。 この結果が -1 の場合は、返されるメッセージが切り詰められることはありません。 これがデフォルトの挙動です。
返されるメッセージのサイズを制限するには Zend_Validate::setMessageLength() を使用します。 必要に応じて任意の整数値を設定します。 返されるメッセージのサイズがここで設定した長さを超えると、 メッセージが切り詰められて最後に文字列 '...' が付加されます。
- Zend_Validate::setMessageLength(100);
注意: ここで設定したメッセージ長はすべてのバリデータに適用されることに注意しましょう。 自前で定義したバリデータに関しても、それが Zend_Validate_Abstract を継承したものである限りは同じです。
| バリデータの書き方 |
