Html :: error似乎不适用于ajaxValidation或clientsidevalidation

sav*_*ode 5 php yii2

我一直在从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>
Run Code Online (Sandbox Code Playgroud)

小智 0

您应该能够使用ActiveRecord 类的getErrors()函数获取这些信息。对于你的代码,你想要这样的东西;

echo $model->getErrors('username');
Run Code Online (Sandbox Code Playgroud)

这将返回与该字段相关的一系列错误。即使只有一条错误消息,它也会返回一个数组