Yii Framework Forum

Accessing the error state of an activeForm input in js


(Joel Small) #1

Hi,

I’m attempting to determine whether or not a specific field has an error state in a multi-step form. I’ve decided to abandon my approach and go with a form validated via ajax for a few reasons, but it did raise an interesting question for me.

I would have thought that I should be able to access the individual error/valid state of a field, something like the following:

$("#step1-submit-button").on("click", 
   
   function (){
   
        attributes = [
            'signupform-username',
            'signupform-password',
            'signupform-email',
        ];
   
        $.each(attributes, function (index, value){
            hasError = $('#form-signup').yiiActiveForm('validateAttribute', value);
            if (hasError) {
                alert('you have an error, fix before continuing');
            }        
        });

  });

I tried a multitude of ways, however on inspection of the form and input elements, it doesn’t appear that the individual fields store an error state (at least not that i can determine).

Does anyone know if it’s possible to get individual field error states in the client-side activeForm.js?

Edit: I should point out that philosophically speaking, I know I could access the output from the validator in the DOM itself (eg, search for an error class applied to an attribute). I wonder what would be considered the cleanest approach? Checking for the output, while functional, seems to be the long way around to get the outcome. I feel like i should be able to call an individual field’s validator, and retrieve whether or not that validator returns a TRUE or FALSE, similar to how the backend validators work.


#2

Does this help: https://github.com/samdark/yii2-cookbook/blob/master/book/forms-activeform-js.md


(Joel Small) #3

Not quite. I did refer to the cookbook first. It shows how to trigger and update validations, but not how to get their result (eg, a validation passed and there were no errors)