php – 如何在yii2框架中使用动态模型时设置属性标签?
作者:互联网
在yii2框架中使用动态模型时如何设置属性标签?
这是我的代码如下:
$model = DynamicModel::validateData(compact('name','shipping'), [
[['name','shipping'], 'required'],
]);
if ($model->hasErrors()) {
// validation fails
// }
} else {
//validation true
}
解决方法:
作为一个简单的出路,只需为所需属性设置单独的验证消息:
$model = DynamicModel::validateData(compact('name','shipping'), [
[['name'], 'required', ['message' => Yii::t('app', 'Name is required for this form.')]],
[['name'], 'required', ['message' => Yii::t('app', 'Shipping address is required for this form.')]],
]);
输出DynamicModel字段时使用标签:
echo $form->field($model, 'name')->textInput()->label(Yii::t('app', 'Name'));
echo $form->field($model, 'name')->textInput()->label(Yii::t('app', 'Shipping address'));
标签:php,yii2,yii2-advanced-app 来源: https://codeday.me/bug/20190628/1315180.html