angularjs - What is the "Angular" way to handle form validation? -
so, i'm looking @ piece of code:
function checkformelements(field) { if ($scope.options === undefined || $scope.optionsform.$error[field] === undefined ) { return false; } var result = false; $.each($scope.optionsform.$error[field], function(index, value) { // todo: there *has* better way element. angular must know form control, whether it's named or not. var element = $('[name=' + value.$name + ']'); if (!element.prop('disabled')) { result = true; } }); return result; } $scope.formisinvalid = function() { return checkformelements("pattern"); }; $scope.fieldisempty = function() { if ($scope.options === undefined || $scope.formisinvalid()) { return false; } return checkformelements("required"); };
and i'd quite clean todo.
the form elements generated in directive by:
template: '\ <input class="output-input" id="{{fieldname}}-input" name="{{fieldname}}" type="text" maxlength="64" ng-model="item.name" ng-pattern="pattern" ng-required="true" ng-trim="false" ng-disabled="item.save===false">\ <div class="save-column" ng-if="showsave"><input type="checkbox" ng-model="item.save" ng-disabled="item.save === undefined" ng-checked="(item.save === undefined) || item.save"></div>\ ',
the basic functionality is:
- there multiple edit fields save box next them.
- if fields have junk in them, form field $error.pattern exists. if field empty, $error.required exists
- we loop on kind of errors, check if element generating enabled (because disabled boxes can hold junk content or blank) , if declare form invalid , not fit sent.
it feels there's way solve that's more in angular's style, , i'd know is.
you need can see properties of form giving name. render form name see angularjs know it.
<form name="myform"> </form> {{myform}}
i think should require ngmodel, i'm not sure you're doing here. add fiddle or picture?
Comments
Post a Comment