<pre class="wp-block-syntaxhighlighter-code">
api\modules\v1\models\PlanTask Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 3
[group_id] => 015ce30b116ce86058fa6ab4fea4ac63
[config_column_id] => 1
[plan_id] => 1
[sort_order] => 1
[title] => 无线成都1
[config_task_id] => 1
[create_user_id] => 8
[create_name] => 13281105967
[exec_user_id] => 185
[exec_name] => test1
[place] => 地点
[task_info] =>
<p>选题摘要</p>
[task_data] => a:0:{}
[ended_at] => 1530695128
[current_step_id] => 18
[prev_status] => 2
[status] => 1
[created_at] => 1528103128
[updated_at] => 1528188754
)
]
)
</pre>
use yii\base\DynamicModel;
class VideoEditAction extends Action
{
const TOOL_JOVE = 'jove'; //工具:Jove微编
const TOOL_NOVA = 'nova'; //工具:Nova精编
// 临时验证
$request = Yii::$app->request;
$tool = $request->get('tool');
$dynamicModel = new DynamicModel(compact('tool'));
$dynamicModel->addRule('tool', 'required')
->addRule('tool', 'in', ['range' => [self::TOOL_JOVE, self::TOOL_NOVA]])
->validate();
if ($dynamicModel->hasErrors()) {
$response = Yii::$app->getResponse();
$response->setStatusCode(422, 'Data Validation Failed.');
foreach ($dynamicModel->getFirstErrors() as $message) {
$firstErrors = $message;
break;
}
return ['code' => 20004, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '20004'), ['firstErrors' => $firstErrors]))];
}
}

{
"code": 20004,
"message": "数据验证失败:Tool不能为空。"
}
![在 Postman 中测试,当 tool 参数不等于([jove, nova])时,符合预期](https://media.shuijingwanwq.com/2018/06/2-2.png)
{
"code": 20004,
"message": "数据验证失败:Tool是无效的。"
}
PHP / Laravel / Yii2 老项目维护与长期技术支持
如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。
适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护
可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查
如需咨询,请联系我,并注明:PHP 维护咨询。
联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

发表回复