robfletcher / grails-angular-scaffolding

A Grails plugin for scaffolding views using Angular.js
http://grails-ng.cloudfoundry.com/
87 stars 43 forks source link

Bind Grails server side validation to Angular's errors model #15

Open robfletcher opened 12 years ago

robfletcher commented 12 years ago

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 commented 12 years ago

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

$scope.form.title.$setValidity('unique', false);
robfletcher commented 12 years ago

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

davispw commented 12 years ago

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
                });
            }
        }
    };
});