yii2数据验证

use yii\base\DynamicModel;

$model = DynamicModel::validateData(compact('id', 'content'), [
    [['id', 'content'], 'required', 'message' => '必须输入内容'],
    [['id'], 'number'],
    [['content'], 'string', 'min' => 1, 'max' => 255, 'message' => '内容长度应在1-255个字符之间'],
]);
if ($model->hasErrors()) {
    return $this->error($model->getErrorSummary(true)[0]);
}

此处评论已关闭