このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
apricot:usage:ja:validation [2020/08/28 08:00] y2sunlight [ValidatorErrorBagクラス] |
apricot:usage:ja:validation [2020/09/03 13:46] y2sunlight [Apricot バリデーション] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > 編集中 | ||
- | |||
- | ---- | ||
- | |||
====== Apricot バリデーション ====== | ====== Apricot バリデーション ====== | ||
--- // | --- // | ||
行 21: | 行 17: | ||
* Apricot バリデーション | * Apricot バリデーション | ||
* [[apricot: | * [[apricot: | ||
+ | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
行 196: | 行 193: | ||
\\ | \\ | ||
- | |||
- | |||
- | |||
- | \\ | ||
- | |||
===== ValidatorErrorBagクラス ===== | ===== ValidatorErrorBagクラス ===== | ||
行 233: | 行 225: | ||
</ | </ | ||
- | '' | + | '' |
\\ | \\ | ||
行 239: | 行 231: | ||
===== バリデーションの使用例 ===== | ===== バリデーションの使用例 ===== | ||
- | インターセプターメソッドのシグネチャには次の規則があります: | + | バリデーションの使用例として、ユーザインターセプター( UserInterceptor )のinsertメソッドを以下に示します。このメソッドはユーザコントローラのinsertアクションのインターセプターとして動作します。 |
- | * **interceptorMethod( Controller $controller, | + | {{fa> |
- | - $controller --- インターセプターを呼び出したコントローラ | + | |
- | - $... --- インターセプターを呼び出したアクションメソッドと同じ引数 | + | |
- | * 戻り値 | + | |
- | - 成功の場合 --- void を返す | + | |
- | - 失敗の場合 --- Responseオブジェクトを返す | + | |
- | + | ||
- | 以下にユーザインターセプター( UserInterceptor )を示します。 | + | |
- | + | ||
- | {{fa> | + | |
<code php UserInterceptor.php> | <code php UserInterceptor.php> | ||
<?php | <?php | ||
- | namespace App\Controllers\Interceptors; | ||
- | |||
- | use Apricot\Input; | ||
- | use App\Foundation\Controller; | ||
- | use App\Foundation\ValidatorErrorBag; | ||
- | |||
/** | /** | ||
- | | + | * Interceptor |
+ | * | ||
+ | * @return void|\Apricot\Foundation\Response return Response if failed | ||
*/ | */ | ||
- | class UserInterceptor | + | public function insert(Controller $controller) |
{ | { | ||
- | | + | |
- | * Interceptor for insert method. | + | |
- | * | + | // Validation |
- | * @return void|\Apricot\Foundation\Response return Response if failed | + | $v =(new \Valitron\Validator($inputs)) |
- | */ | + | |
- | | + | |
+ | -> | ||
+ | -> | ||
+ | -> | ||
+ | -> | ||
+ | -> | ||
+ | |||
+ | if(!$v-> | ||
{ | { | ||
- | $inputs | + | $errorBag |
+ | return redirect(back())-> | ||
+ | } | ||
- | | + | |
- | $v =(new \Valitron\Validator($inputs)) | + | |
- | | + | } |
- | -> | + | </code> |
- | -> | + | |
- | -> | + | |
- | -> | + | |
- | | + | |
- | ->labels(inputLabels(' | + | |
- | if(!$v-> | + | この例では、Input:: |
- | { | + | |
- | $errorBag = new ValidatorErrorBag($v-> | + | |
- | return redirect(back())-> | + | |
- | } | + | |
- | // Removes unnecessary inputs | + | === バリデーションルールの適用 === |
- | Input:: | + | |
- | } | + | |
- | /** | + | バリデーションルールの適用には、'' |
- | * Interceptor for update method. | + | |
- | * | + | |
- | * @param int $id | + | |
- | * @return void|\Apricot\Foundation\Response return Response if failed | + | |
- | */ | + | |
- | public function update(Controller $controller, | + | |
- | { | + | |
- | $inputs = Input::all(); | + | |
- | // Validation | + | * '' |
- | $v =(new \Valitron\Validator($inputs)) | + | * '' |
- | ->rule('ascii','password') | + | * '' |
- | | + | * '' |
- | | + | * '' |
- | -> | + | * '' |
- | if(!$v-> | + | この他の使用できるルールについては、Valitronの[[https:// |
- | { | + | |
- | $errorBag = new ValidatorErrorBag($v-> | + | |
- | return redirect(back())-> | + | |
- | } | + | |
- | // Removes unnecessary inputs | + | === 入力変数のラベリング === |
- | Input:: | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | バリデーションの手順は以下の通りです。 | + | '' |
- | | + | {{fa> |
- | | + | <code php messages.php> |
- | | + | <?php |
- | | + | return [ |
- | | + | |
- | | + | ' |
- | * withInputs()で入力変数をフラッシュ変数に保存します | + | ' |
- | * withErrors()でバリデーションのエラーバッグをフラッシュ変数に保存します | + | ' |
- | * redirect()で前画面にリダイレクトするResponseオブジェクトをします | + | ' |
- | | + | ' |
- | * 不要になったバリデーション用のフォームデータを削除します | + | ], |
+ | | ||
+ | ]; | ||
+ | </ | ||
- | === 検証ルール === | + | 言語ファイル及び言語メッセージについては、[[apricot: |
- | * **insert()** : レコード挿入時\\ | + | === バリデーションの実行 === |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * **update()** : レコード更新時\\ | + | バリデーションの実行には、'' |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
\\ | \\ | ||