I try to write a dynamic validation. if a check box is checked a new 
section is shown in my page. As a result, in the new section some new 
inputs are required. However, the check box is not checked only one input 
in the main form in mandatory. 


I write my costume directive in the following. my problem is in the bold 
line. I know we can access to scope variable dynamically by 
$scope["string"] but in directive we cannot. please guide me?


  <input .... multiValidation="Street,City,PostalCode" />


app.directive('multiValidation', function () {
    return function (scope, iElement, iAttrs) {
       var parts = iAttrs.myvalidate.split(',');
           scope.$watch('CustomerModel.Billable' // the check box scope 
variable , function (val) {
        if (scope.CustomerModel.Billable) {


            angular.forEach(parts, function (part) {

                var element = angular.element('[ng-model="CustomerModel.' + 
part + '"]');

                scope.$watch('CustomerModel.' + part, function (value) {
                    if (value == null || value == "") {
                        scope.CustomerForm.$setValidity("CustomerForm", false);
                        element.addClass("ng-invalid ng-invalid-required");
                    }
                    else {

                        element.addClass("ng-valid ng-valid-required");
                        var validate = true;
                        angular.forEach(parts, function (part) {

                            *if (scope["CustomerModel." + part ] == "")*
                                validate = false;

                        });

                        scope.CustomerForm.$setValidity("CustomerForm", 
validate);
                    }
                });




            });

        } else {

               // the main input which is mandatory 
            if (scope.CustomerModel.LastName == "" || 
scope.CustomerModel.LastName == null)
                scope.CustomerForm.$setValidity("CustomerForm", false);
            if (scope.CustomerModel.LastName != "" || 
scope.CustomerModel.LastName != null)
                scope.CustomerForm.$setValidity("CustomerForm", true);
        }
    }, true);};});

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to