grails-angular-scaffolding icon indicating copy to clipboard operation
grails-angular-scaffolding copied to clipboard

Bind Grails server side validation to Angular's errors model

Open robfletcher opened this issue 12 years ago • 3 comments

See: [http://docs.angularjs.org/api/ng.directive:form]

Angular uses a different structure on the form controller field.$error.type, e.g. title.$error.required

It would be nice to bind errors from server side validation to this structure so the display could work the same way for both.

robfletcher avatar Jun 25 '12 16:06 robfletcher

This can be done in a controller with a call such as

$scope.form.title.$setValidity('unique', false);

robfletcher avatar Jun 26 '12 06:06 robfletcher

The problem is that the validity state needs to be recomputed after the user changes the form value.

robfletcher avatar Jun 26 '12 08:06 robfletcher

Directive to clear errors when the user changes the form value. If by "recomputed" you mean actually re-submit to the server then try this answer instead of mine: http://stackoverflow.com/questions/12864887/angularjs-integrating-with-server-side-validation

<input type="text" ... validated>
angular.module('serverSideValidation', []).directive('validated', function() {
    return {
        restrict: 'A',
        require: '?ngModel',
        link: function(scope, elem, attr, ctrl) { 
            if (attr.ngModel) {
                scope.$watch(attr.ngModel, function(value) {
                    ctrl.$setValidity('unique', true); // <<< or whatever your error key is
                });
            }
        }
    };
}); 

davispw avatar Oct 19 '12 14:10 davispw