1

i am newly using valdr in angualrjs . i m making validation directive using valdr for my application. i don't want to write addconstaints and addvalidator functions with json inside app.config file. i want to write that part inside the directive only ? can anyone solve this problem...?

var app = angular.module('app', ['valdr']);

app.config(function (valdrProvider) {
    valdrProvider.addConstraints({
        'Person': {
            'firstname':
                {
                    'size': {
                        'min': 3,
                        'max': 12,
                        'message':'firstname should be between 3 to 12 '
                    },
                    'required': {
                        'message': 'This field is required.'
                    }
                },
            'firstName':
                {
                    'customValidator': {
                        'message': 'First name must be Hanueli.'
                    }
                }
        }
    });
});

app.directive('info', function () {
    return {
        restrict: 'E',
        templateUrl: 'tmpl.html',
        require: '^?valdrProvider',
        link: function ($scope, valdrProvider) {
            $scope.Person = {};
            $scope.$watch(valdrProvider.getConstraints, function (newContraints) {
                $scope.constraints = newContraints;
            })
        }
    };
});
bhaskarsd
  • 11
  • 1

2 Answers2

0

I don't see how that could be useful but you can of course call valdrProvider.addConstraints() whenever you see fit. In link: function ($scope, valdrProvider) you get access to the provider.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • thank you sir for your valuable feedback .. but i tried it not working. if you some other way plz let me know.. – bhaskarsd Feb 12 '16 at 05:17
0

The issue is with the naming. Angular provides two ways to configure providers. 1. during the configuration phase - here you have to append the "Provider" suffix. 2. during runtime - here you just get the service.

Change valdrProvider to valdr and you can addConstraints in your service/controller

Hellmy
  • 76
  • 6