yii2
カスタム検証
サーチ…
前書き
Yii2には、プログラム関連の一般的な子犬の検証を解決する際に使用できる組み込みのバリデータがいくつかあります。新しいビジネスロジック検証を作成する必要がある場合は、独自の検証ツールを作成する必要があります。
検証の種類
初めに基本バリデータの種類を理解しましょう:
- インラインバリデーター
- スタンドアロン検証ツール
インラインバリデーター :クラス内に作成するバリデータの型です。基本的には他のメソッドと同様に定義しますが、Yii2によって渡される余分なパラメータがあります。
....
public function ValidateMyBusiness($attr, $params){
// adding an error here means our validation is failed.
if ($this->{$attr} > 1100) {
$this->addError($attr, "Some error occured");
}
}
...
// calling above validator is simple as below:
public function rules(){
return [
['money', 'validateMyBusiness', 'params' => ['targetAccount' => $this->account]];
]
}
# params array will be passed to our inline parameter as a second argument.
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow