forms - AngularJS Validation ngModel undefined -


i have form validations dynamically added form response web service call. when call returns tells validation directive validations need add form. (i because want reuse same validations on server during submit on client.) works wonderfully when validations of type "required". the problem have when model value of form control not pass the validation rules, model value "undefined". therefore nothing get's sent server on form submission validate on server side. realize block form submission if form not valid, however, letting server determine validity of data comes across.

what force model value "invalid value" regardless if violated validation rule? better suggestions? below snipit of directive using.

    //this directive should put on ng-form element , hide/show 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 new errors                     (var key in errors) {                         if (key.indexof('__') == 0)                             continue;                          _.each(errors[key], function (obj) {                             if (form[obj.$name] == obj || ignorechecking) { //ensure obj current form                                 var input = vs.findelementbyname(element, obj.$name);                                 if (input.length > 0) {                                     func(input, obj);                                 }                             }                         });                     }                 }                  scope.$watch(function () { return form.$error; }, function (newval, oldval, scp) {                     iterateovererrorsobject(oldval, function (input, obj) {                         vs.hideerrors(input);                     }, true);                     iterateovererrorsobject(newval, function (input, obj) {                         vs.showerrors(input, obj, form._attr);                     });                 }, true);                  //something told validator show it's errors                 scope.$on('show-errors', function (evt) {                     iterateovererrorsobject(form.$error, function (input, obj) {                         vs.showerrors(input, obj, form._attr);                     });                 });                  scope.$on('hide-errors', function (evt) {                     vs.hideallerrors(form);                 });             }         };     }])     //this directive put on ng-form element , dynamically add/remove validators based on validations configuration     //which comes service call "validate"     .directive('dynamicvalidators', ['$compile', function ($compile) {         return {             priority: 0,             restrict: 'a',             //require: 'ngmodel',             require: '^form',             scope: {                 'validations': '=',             },             link: function (scope, element, attrs, ctrl) {                 (function (form, scp) {                      // hold information necessary error message displayed                     // **have add form because ctrl gets recreated every time form.$error changes                     function setattr(ctrl, key, value) {                         if (!ctrl._attr)                             ctrl._attr = {};                         if (!form._attr)                             from._attr = {};                          ctrl._attr[key] = value;                         var obj = form._attr[ctrl.$name] = {};                         obj[key] = value;                     };                      scope.$watch('validations', function (nv, ov) {                         form._attr = {};                         //remove old validators                         if (ov && ov.length > 0) {                             _.each(ov, function (e) {                                 var fctrl = form[e.membernames[0]];                                 if (fctrl && fctrl.$validators) {                                     delete fctrl.$validators[e.errorkey];                                     //fctrl.$setvalidity(e.errorkey, true);                                     fctrl.$validate();                                 }                             });                         }                          //add new validators                         if (nv && nv.length > 0) {                             _.each(nv, function (e) {                                 var fctrl = form[e.membernames[0]];                                 if (!fctrl)                                     return;                                  if (e.errorkey == 'required') {                                     setattr(fctrl, e.errorkey, e.errorvalue);                                     fctrl.$validators[e.errorkey] = function (modelvalue, viewvalue) {                                         if (modelvalue instanceof array)                                             return modelvalue.length > 0;                                         else                                             return modelvalue !== '' && modelvalue !== null && modelvalue !== undefined;                                     };                                 } else if (e.errorkey == 'alphanumeric') {                                     setattr(fctrl, e.errorkey, e.errorvalue);                                     fctrl.$validators[e.errorkey] = function (modelvalue, viewvalue) {                                         return viewvalue == null || (viewvalue != null && /^[a-za-z0-9]*$/.test(modelvalue));                                     };                                 } else if (e.errorkey == 'min') {                                     setattr(fctrl, e.errorkey, e.errorvalue);                                     fctrl.$validators[e.errorkey] = function (modelvalue, viewvalue) {                                         return modelvalue === undefined || modelvalue === null || modelvalue === "" || modelvalue >= e.errorvalue;                                     }                                 } else if (e.errorkey == 'max') {                                     setattr(fctrl, e.errorkey, e.errorvalue);                                     fctrl.$validators[e.errorkey] = function (modelvalue, viewvalue) {                                         return modelvalue === undefined || modelvalue === null || modelvalue === "" || modelvalue <= e.errorvalue;                                     }                                 }                                  //make validator fire set status of validator                                 if (fctrl.$validators[e.errorkey])                                     //fctrl.$setvalidity(e.errorkey, fctrl.$validators[e.errorkey](fctrl.$modelvalue, fctrl.$viewvalue))                                     fctrl.$validate();                             });                         }                     });                 })(ctrl, scope);             },         }     }]); 

if still want send server invalid data, can use allowinvalid:

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

it's option you.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -