Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
drinkncode opened this issue Sep 16, 2015 · 16 comments

Comments

@drinkncode
Copy link

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:

'help-block']); ?>

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?

@samdark
Copy link
Member

samdark commented Sep 16, 2015

@samdark samdark closed this as completed Sep 16, 2015
@drinkncode
Copy link
Author

@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,

'help-block']); ?>

is not working on ajaxValidation.

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

@samdark
Copy link
Member

samdark commented Sep 16, 2015

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.

@drinkncode
Copy link
Author

@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?

@cebe
Copy link
Member

cebe commented Sep 16, 2015

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

@drinkncode
Copy link
Author

@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.

@samdark
Copy link
Member

samdark commented Sep 17, 2015

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

@drinkncode
Copy link
Author

@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

@samdark
Copy link
Member

samdark commented Sep 17, 2015

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

@drinkncode
Copy link
Author

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..

@samdark
Copy link
Member

samdark commented Sep 17, 2015

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

@drinkncode
Copy link
Author

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.

@samdark
Copy link
Member

samdark commented Sep 17, 2015

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

@drinkncode
Copy link
Author

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.

@samdark
Copy link
Member

samdark commented Sep 19, 2015

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

@samdark
Copy link
Member

samdark commented Sep 19, 2015

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

@samdark samdark closed this as completed Sep 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants