hewstone February 2016

AngularJS Validation ngModel undefined

I have my form validations dynamically added to the form from a response to a web service call. When the call returns it tells my validation directive what validations I need to add to the form. (I do this because I want to reuse the same validations on the server during submit as I do on the client.) This works wonderfully when the only validations are of type "required". The problem I have is when the model value of the form control does not pass the the validation rules, the model value is then "undefined". Therefore nothing get's sent to the server on form submission to validate on the server side. I do realize I could block the form submission if the form is not valid, however, I am letting the server determine the validity of the data that comes across.

What could I do to force the model value to be the "invalid value" regardless if it violated a validation rule? Better suggestions? Below is a snipit of my directive I am using.

    //this directive should be put on an ng-form element and it will hide/show any validations set on each input
    .directive('formValidator', ['validatorService', function (vs) {
        return {
            restrict: 'A',
            require: '^form',
            link: function (scope, element, attrs, form) {

                function iterateOverErrorsObject(errors, func, ignorechecking) {
                    if (!func)
                        return;

                    //show any new errors
                    for (var key in errors) {
                        if (key.indexOf('__') == 0)
                            continue;

                        _.each(errors[key], function (obj) {
                            if (form[obj.$name] == obj || ignorechecking) { //ensure the obj is for the current form
                                var input = vs.findElementByName(element, obj.$name);
                                if (input.length > 0) {
                            

Answers


Stepan Kasyanenko February 2016

If you still want to send to the server invalid data, you can use the allowInvalid:

<input type="text" name="userName"
         ng-model="user.name"
         ng-model-options="{ allowInvalid: true }"" />

It's option help you.

Post Status

Asked in February 2016
Viewed 2,535 times
Voted 4
Answered 1 times

Search




Leave an answer