Yii2: Html::error doesn't seems to be working on ajaxValidation or clientsidevalidation

Created on 16 Sep 2015  路  16Comments  路  Source: yiisoft/yii2

Hi, i have been shifting from yii 1.1 and yii 2.0, and i have to display my attribute error at the top of the form and no inline errors, but finding it hard to so, to achieve my requirement i tried putting below code at the top of my form:

but it doesn't display any errors on ajaxValidation and clientValidation while on normal submit and validate it is showing up the error.

Am i doing something wrong or is there an alternative to achieve this or is this missed or left out?

need more info

All 16 comments

It should be active form error summary: http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html#errorSummary()-detail

@samdark i don't want to display the error summary, i just want to display the error of a particular attribute say "username", which i am not able to find a way to achieve,

is not working on ajaxValidation.

errorSummary($model); ?> displays all the errors of the form, and why is this closed i wonder..

Client validation works with Active Form only so any Html:: won't work.

A field for a single attribute rendered via http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html#field()-detail will display error for that attribute.

@samdark: Yes it displaying error for that attribute, but only inline, that is below the field itself which i already know, but not at the top of the form..
Look here, explaining my scenario with the help of some code..

my form with Ajax Validation (Read the comment please):

<?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>
</div>
<?php ActiveForm::end(); ?>

I think this is a general requirement, someone may want to display errors for certain attributes only or in my case only single attribute, and that too at the top of the form while using ajax validation, how do we achieve this in Yii 2.0, finding it hard..can you please suggest?

Please use the forum for asking such questions, github is for bug and feature requests.

@cebe Apologies, but it looked like a feature being ignored in yii 2.0 while it was there in yii 1.1..hence i raised this question here. Googled the same thing didn't found the solution for this. Updated the same already on different forums, but no one seems to have the answer.

@drinkncode is that real requirement in your app to display error for exactly one field at the top of the form?

@samdark Yes, i can even provide you an screenshot, we are already doing this in our web app which is on Yii 1.1..and this is not possible in Yii 2.0..Its designed that way, and now if its unavailable, that will be major design change for us, and like i said this can be a requirement for many..
Here is the screenshot please check, its on Yii 1.1..
2015-09-17_1755

Umm... but summary should work in this case.

But summary doesn't accept any parameters, which can limit the errors to a particular attribute, like..

  <?= $form->errorSummary($model, ['attributes'=>['username']]); ?> // Not applicable

can we have this, and if nothing is passed it can show all the errors.. Or if possible please add method "error" back in the class ActiveForm.just like yii 1.1 used to have..this will be lot easier and helpful..

What if email is filled correctly but password isn't filled?

It says "Email/Password can't be empty", which we show that to user by adding that error in "username" attribute..by the code below..

 $this->addError('username', 'Email/Password can't be empty');

and if login credentials are wrong we display that using..

 $this->addError('username', 'Invalid username or password');

also when user clicks on forgot password we validate the username only and can display message for email only not for password.

OK but then summary won't display multiple lines since there are no multiple errors...

Ok here is the catch, there will always be two errors, when we have entered invalid email id or special characters or something else, i.e
"Email id is not a valid email address"
"Email/Password can't be empty"

where errorSummary will display all at the same time, but this->error("username"), queue them and always display error at the 0 index, i.e first value of the array.

If these are for the same attribute and you haven't touched skipOnError (it's true by default) then it will be one error.

It's a very bad idea to use one field error instead of summary.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AstRonin picture AstRonin  路  3Comments

indicalabs picture indicalabs  路  3Comments

jpodpro picture jpodpro  路  3Comments

MUTOgen picture MUTOgen  路  3Comments

Locustv2 picture Locustv2  路  3Comments