Commit 9a51c5fa by Nobuo Kihara

docs/guide-ja/tutorial-core-validators.md - WIP [ci skip]

parent bdda4304
...@@ -240,63 +240,67 @@ function foo($model, $attribute) { ...@@ -240,63 +240,67 @@ function foo($model, $attribute) {
```php ```php
[ [
// "primaryImage" が PNG、JPG、または GIF 形式のアップロードされた // "primaryImage" が PNG、JPG、または GIF 形式のアップロードされた
// 画像ファイルであり、ファイルサイズが 1MB 未満であることを検証 // 画像ファイルであり、ファイルサイズが 1MB 以下であることを検証
['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024], ['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024],
] ]
``` ```
このバリデータは、入力が有効なアップロードされたファイルであることを検証します。 このバリデータは、入力値がアップロードされた有効なファイルであることを検証します。
- `extensions`: a list of file name extensions that are allowed to be uploaded. This can be either - `extensions`: アップロードを許可されるファイル名拡張子のリスト。
an array or a string consisting of file extension names separated by space or comma (e.g. "gif, jpg"). リストは、配列、または、空白かカンマで区切られたファイル名拡張子からなる文字列 (例えば、"gif, jpg") で指定することが出来ます。
Extension names are case-insensitive. Defaults to null, meaning all file name 拡張子名は大文字と小文字を区別しません。
extensions are allowed. デフォルト値は null であり、すべてのファイル名拡張子が許可されることを意味します。
- `mimeTypes`: a list of file MIME types that are allowed to be uploaded. This can be either an array - `mimeTypes`: アップロードを許可されるファイルの MIME タイプのリスト。
or a string consisting of file MIME types separated by space or comma (e.g. "image/jpeg, image/png"). リストは、配列、または、空白かカンマで区切られたファイルの MIME タイプからなる文字列 (例えば、"image/jpeg, image/png") で指定することが出来ます。
Mime type names are case-insensitive. Defaults to null, meaning all MIME types are allowed. MIME タイプ名は大文字と小文字を区別しません。
- `minSize`: the minimum number of bytes required for the uploaded file. Defaults to null, meaning no lower limit. デフォルト値は null であり、すべての MIME タイプが許可されることを意味します。
- `maxSize`: the maximum number of bytes allowed for the uploaded file. Defaults to null, meaning no upper limit. - `minSize`: アップロードされるファイルに要求される最小限のバイト数。
- `maxFiles`: the maximum number of files that the given attribute can hold. Defaults to 1, meaning デフォルト値は null であり、下限値が無いことを意味します。
the input must be a single uploaded file. If it is greater than 1, then the input must be an array - `maxSize`: アップロードされるファイルに許可される最大限のバイト数。
consisting of at most `maxFiles` number of uploaded files. デフォルト値は null であり、上限値が無いことを意味します。
- `checkExtensionByMimeType`: whether to check the file extension by the file's MIME type. If the extension produced by - `maxFiles`: 指定された属性が保持しうる最大限のファイル数。
MIME type check differs from the uploaded file extension, the file will be considered as invalid. Defaults to true, デフォルト値は 1 であり、入力値がアップロードされた一つだけのファイルでなければならないことを意味します。
meaning perform such check. この値が 2 以上である場合は、入力値は最大で `maxFiles` 数のアップロードされたファイルからなる配列でなければなりません。
- `checkExtensionByMimeType`: ファイルの MIME タイプでファイル拡張子をチェックするか否か。
`FileValidator` is used together with [[yii\web\UploadedFile]]. Please refer to the [Uploading Files](input-file-upload.md) MIME タイプのチェックから導かれる拡張子がアップロードされたファイルの拡張子と違う場合に、そのファイルは無効であると見なされます。
section for complete coverage about uploading files and performing validation about the uploaded files. デフォルト値は true であり、そのようなチェックが行われることを意味します。
`FileValidator`[[yii\web\UploadedFile]] と一緒に使用されます。
ファイルのアップロードおよびアップロードされたファイルのバリデーションの実行に関する完全な説明は、[ファイルをアップロードする](input-file-upload.md) の節を参照してください。
## [[yii\validators\FilterValidator|filter]] <a name="filter"></a> ## [[yii\validators\FilterValidator|filter]] <a name="filter"></a>
```php ```php
[ [
// trim "username" and "email" inputs // "username" と "email" の入力値をトリムする
[['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true], [['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true],
// normalize "phone" input // "phone" の入力値を正規化する
['phone', 'filter', 'filter' => function ($value) { ['phone', 'filter', 'filter' => function ($value) {
// normalize phone input here // 電話番号の入力値をここで正規化する
return $value; return $value;
}], }],
] ]
``` ```
This validator does not validate data. Instead, it applies a filter on the input value and assigns it このバリデータはデータを検証しません。
back to the attribute being validated. 代りに、入力値にフィルタを適用して、それを検証される属性に書き戻します。
- `filter`: a PHP callback that defines a filter. This can be a global function name, an anonymous function, etc. - `filter`: フィルタを定義する PHP コールバック。
The function signature must be `function ($value) { return $newValue; }`. This property must be set. これには、グローバル関数の名前、無名関数などを指定することが出来ます。
- `skipOnArray`: whether to skip the filter if the input value is an array. Defaults to false. 関数のシグニチャは ``function ($value) { return $newValue; }` でなければなりません。
Note that if the filter cannot handle array input, you should set this property to be true. Otherwise some このプロパティは必須項目です。
PHP error might occur. - `skipOnArray`: 入力値が配列である場合にフィルタをスキップするか否か。
デフォルト値は false。
フィルタが配列の入力を処理できない場合は、このプロパティを true に設定しなければなりません。
そうしないと、何らかの PHP エラーが生じ得ます。
> Tip: If you want to trim input values, you may directly use the [trim](#trim) validator. > Tip|ヒント: 入力値をトリムしたい場合は、[trim](#trim) バリデータを直接使うことが出来ます。
> Tip: There are many PHP functions that have the signature expected for the `filter` callback. > Tip|ヒント: `filter` のコールバックに期待されるシグニチャを持つ PHP 関数が多数存在します。
> For example to apply type casting (using e.g. [intval](http://php.net/manual/en/function.intval.php), > 例えば、型キャストを適用して (例えば、[intval](http://php.net/manual/ja/function.intval.php) や [boolval](http://php.net/manual/ja/function.boolval.php) などを使って) 属性が特定の型になるように保証したい場合は、それらの関数をクロージャで包む必要はなく、単にフィルタの関数名を指定するだけで十分です。
> [boolval](http://php.net/manual/en/function.boolval.php), ...) to ensure a specific type for an attribute,
> you can simply specify the function names of the filter without the need to wrap them in a closure:
> >
> ```php > ```php
> ['property', 'filter', 'filter' => 'boolval'], > ['property', 'filter', 'filter' => 'boolval'],
...@@ -308,7 +312,7 @@ back to the attribute being validated. ...@@ -308,7 +312,7 @@ back to the attribute being validated.
```php ```php
[ [
// checks if "primaryImage" is a valid image with proper size // "primaryImage" が適切なサイズの有効な画像であることを検証
['primaryImage', 'image', 'extensions' => 'png, jpg', ['primaryImage', 'image', 'extensions' => 'png, jpg',
'minWidth' => 100, 'maxWidth' => 1000, 'minWidth' => 100, 'maxWidth' => 1000,
'minHeight' => 100, 'maxHeight' => 1000, 'minHeight' => 100, 'maxHeight' => 1000,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment