我一直在从yii 1.1和yii 2.0转移,我必须在表单的顶部显示我的属性错误,没有内联错误,但发现很难,为了达到我的要求,我尝试将代码放在下面我的表格顶部:
<?= Html::error($model, 'username',['class'=>'help-block']); ?>
但在正常提交时,它并未在ajaxValidation
和clientValidation
上显示任何错误,并且验证它是否显示错误。
我做错了什么,或者是否有其他方法可以实现这一目标,还是错过了或遗漏了?
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'validateOnSubmit' => true,
'validateOnChange' => false,
'validateOnType' => false
]); ?>
// Error for username attribute here is not showing up, on validating with ajax,
<?= Html::error($model, 'username', array('class' => 'loginerrors help-block')); ?>
// how to show the error here..?? In yii 1.1 we simply call $form->error($model, "username") but here there is nothing like it.
<div class="loginForm" id="login1">
<div class="container">
<div class="row">
<div class="span4 offset4 control-group" id="username_offset">
<?= $form->field($model, 'username')->textInput(); ?>
//Not here, it is displaying the error here on ajax validation, by forming divs dynamically.
</div>
<div class="span4 control-group">
<?= $form->field($model, 'password')->passwordInput(); ?>
</div>
<div class="span12">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
</div>
答案 0 :(得分:0)
您应该能够使用ActiveRecord类的getErrors()函数来获取这些函数。对于你的代码,你需要这样的东西;
echo $model->getErrors('username');
这将返回与该字段相关的一系列错误。即使只有一条错误消息,它也会返回一个数组