jonsamwell/angular-auto-validate

Inputs are validated even when outside a form

Closed this issue · 5 comments

First - awesome work creating this!

My issue:
Is there an option to only validate inputs inside a form?

I'm using bootstrap 3 and the console keeps logging "Angular-auto-validate: invalid bs3 form structure elements must be wrapped by a form-group class" for all fields that are not wrapped inside the "form-group" class (duh). But the thing is that I do not want to validate those fields at all (and therefore have not placed them inside a form).

I've found a workaround at the moment but I don't know if it's the best solution:
In angular.module('jcs-autoValidate').config I've added:

if (frmCtrl !== null) { //added check to see if the element is inside a form
    if (attrs.formnovalidate === undefined || (frmCtrl !== undefined && frmCtrl.disableDynamicValidation === false)) {
        if (supportsNgModelOptions || ngModelOptions === undefined || ngModelOptions.updateOn === undefined || ngModelOptions.updateOn === '') {
            ngModelCtrl.$setValidity = function(validationErrorKey, isValid) {
                setValidity.call(ngModelCtrl, validationErrorKey, isValid);
                setValidationState();
            };
        } else {
            element.on(ngModelOptions.updateOn, function() {
                setValidationState();
            });

            scope.$on('$destroy', function() {
                element.off(ngModelOptions.updateOn);
            });
        }

        // We override this so we can reset the element state when it is called.
        ngModelCtrl.$setPristine = function() {
            setPristine.call(ngModelCtrl);
            validationManager.resetElement(element);
        };

        ngModelCtrl.autoValidated = true;
    }
} //end check

I thought at one point you could add a attribute to display the validation but it must have got refactored out by accident. I'll look to put it back in but for now you could add the attributes to disable the visual styling. see http://jonsamwell.github.io/angular-auto-validate/#enabl-disable-styling

I'll try add add this feature back in in the next week :-)

Perfect, thanks for the quick response!

Adding the attribute disable-valid-styling="true" (to the input)
gives me the error:
TypeError: Cannot read property 'find' of undefined
at reset (jcs-auto-validate.js:288)
at Object.makeDefault (jcs-auto-validate.js:446)
at makeDefault (jcs-auto-validate.js:267)
at makeValid (jcs-auto-validate.js:252)
at Object.validateElement (jcs-auto-validate.js:851)
at jcs-auto-validate.js:1091
at jcs-auto-validate.js:480
at angular.js:17855
at e (angular.js:5507)
at angular.js:5784

angular.forEach(el.find('span'), function (spanEl) {

You can now add disable-auto-validate="true" to the element and it won't validate. You'll need the latest version v1.19.0

I cannot reproduce the above error you are getting. Could you do a plunker for me and I'll fix. Thanks!

I was searching for something like the option disable-auto-validate="true". Is very useful. It's not included in the Demo Page, right? Regards