自定义校验器
Yii提供了一套内置表单校验器,基本覆盖了所有典型的开发需求,并且是高度可配置的。但是,在一些情况下,开发者可能需要创建一个自定义校验器。
本小节会给出一个例子,创建一个检查单词个数的独立校验器。
准备
按照官方指南http://www.yiiframework.com/doc-2.0/guide-start-installation.html的描述,使用Composer包管理器创建一个新的应用。
如何做…
- 创建一个独立校验器
@app/components/WordsValidator.php
:
<?php
namespace app\components;
use yii\validators\Validator;
class WordsValidator extends Validator
{
public $size = 50;
public function validateValue($value){
if (str_word_count($value) > $this->size) {
return ['The number of words must be less than {size}', ['size' => $this->size]];
}
return false;
}
}
- 创建一个
Article
模型@app/models/Article.php
:
<?php
namespace app\models;
use app\components\WordsValidator;
use yii\base\Model;
class Article extends Model
{
public $title;
public function rules()
{
return [
['title', 'string'],
['title', WordsValidator::className(), 'size' =>
10],
];
}
}
- 创建
@app/controllers/ModelValidationController.php
:
<?php
namespace app\controllers;
use app\models\Article;
use yii\helpers\Html;
use yii\web\Controller;
class ModelValidationController extends Controller
{
private function getLongTitle()
{
return 'There is a very long content for current article, '.'it should be less then ten words';
}
private function getShortTitle()
{
return 'There is a shot title';
}
private function renderContentByModel($title)
{
$model = new Article();
$model->title = $title;
if ($model->validate()) {
$content = Html::tag('div', 'Model is valid.',[
'class' => 'alert alert-success',
]);
} else {
$content = Html::errorSummary($model, [
'class' => 'alert alert-danger',
]);
}
return $this->renderContent($content);
}
public function actionSuccess()
{
$title = $this->getShortTitle();
return $this->renderContentByModel($title);
}
public function actionFailure()
{
$title = $this->getLongTitle();
return $this->renderContentByModel($title);
}
}
- 访问
index.php?r=model-validation/success
来运行modelValidation
控制器的success
动作:
- 访问
index.php?r=model-validation/failure
来运行modelValidation
控制器的failure
动作:
- 创建
@app/controllers/AdhocValidationController.php
:
<?php
namespace app\controllers;
use app\components\WordsValidator;
use app\models\Article;
use yii\helpers\Html;
use yii\web\Controller;
class AdhocValidationController extends Controller
{
private function getLongTitle()
{
return 'There is a very long content for current article, '.'it should be less then ten words';
}
private function getShortTitle()
{
return 'There is a shot title';
}
private function renderContentByTitle($title)
{
$validator = new WordsValidator([
'size' => 10,
]);
if ($validator->validate($title, $error)) {
$content = Html::tag('div', 'Value is valid.',[
'class' => 'alert alert-success',
]);
} else {
$content = Html::tag('div', $error, [
'class' => 'alert alert-danger',
]);
}
return $this->renderContent($content);
}
public function actionSuccess()
{
$title = $this->getShortTitle();
return $this->renderContentByTitle($title);
}
public function actionFailure()
{
$title = $this->getLongTitle();
return $this->renderContentByTitle($title);
}
}
- 访问
index.php?r=adhoc-validation/success
来运行adhocValidation
控制器的success
动作:
- 访问
index.php?r=adhoc-validation/failure
来运行adhocValidation
控制器的failure
动作:
工作原理
首先我们创建了一个独立的校验器,它会使用str_word_count
函数来检查单词的数量,然后演示了两个使用例子:
- 作为
Article
模型的校验规则使用这个校验器 - 作为一个特定的校验器使用这个校验器
参考
欲了解更多信息,参考如下链接: