在一个ok项目中,它应该是这样的:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#form-signup').yiiActiveForm({
"username": {
"validate": function(attribute, value, messages) {
yii.validation.required(value, messages, {
"message": "Username\u4e0d\u80fd\u4e3a\u7a7a\u3002"
});
yii.validation.string(value, messages, {
"message": "Username\u5fc5\u987b\u662f\u4e00\u6761\u5b57\u7b26\u4e32\u3002",
"min": 2,
"tooShort": "Username\u5e94\u8be5\u5305\u542b\u81f3\u5c112\u4e2a\u5b57\u7b26\u3002",
"max": 255,
"tooLong": "Username\u53ea\u80fd\u5305\u542b\u81f3\u591a255\u4e2a\u5b57\u7b26\u3002",
"skipOnEmpty": 1
});
},
"id": "signupform-username",
"name": "username",
"validateOnChange": true,
"validateOnType": false,
"validationDelay": 200,
"container": ".field-signupform-username",
"input": "#signupform-username",
"error": ".help-block"
},
</script>
但是在我的项目中,当我输入无效的电子邮件或用户名时,没有提示或错误显示!当我检查yii生成的代码时,我看到yiiActiveForm的空args
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#form-signup').yiiActiveForm([], []);
});
</script>
谁能告诉我为什么?供应商文件夹有什么问题吗?
答案 0 :(得分:0)
这可能是因为您的模型类未设置为在正确的方案中,导致跳过验证。请查看有关此主题的文档:
http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios
如果您希望在当前视图上应用某些规则,则需要在创建模型实例时设置方案:
// scenario is set as a property
$model = new User;
$model->scenario = 'login';
// scenario is set through configuration
$model = new User(['scenario' => 'login']);
在您的模型中,用户上面的案例,您将拥有此功能:
namespace app\models;
use yii\db\ActiveRecord;
class User extends ActiveRecord
{
public function scenarios()
{
return [
'login' => ['username', 'password'],
//more scenarios would follow here...
];
}
}