Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate dynamic form fields in angular directive?

I would like to create form with fields created in directive. Data binding of data working correctly but validation doesn't work.

this is html:

<body ng-controller="MainCtrl">
  <h1>form</h1>
  <form name="form">
      <div ng-repeat="conf in config">
          <div field data="data" conf="conf"></div>
      </div>
  </form>
  <pre>{{data|json}}</pre>
</body>

controller and field directive:

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

app.controller('MainCtrl', function($scope) {
    $scope.data = {name: '', age: ''}
    $scope.config = [
        {field: 'name', required:true},
        {field: 'age'}
    ];
});

app.directive('field', function ($compile) {
    return {
        scope: {
            data: '=',
            conf: '='
        },
        link: function linkFn(scope, element, attrs) {
            // field container
            var row = angular.element('<div></div>');

            // label
            row.append(scope.conf.field + ': ');

            // field input
            var field = angular.element('<input type="text" />');
            field.attr('name', scope.conf.field);
            field.attr('ng-model', 'data.' + scope.conf.field);
            if (scope.conf.required) {
                field.attr('required', 'required');
            }
            row.append(field);

            // validation
            if (scope.conf.required) {
                var required = angular.element('<span>required</span>');
                required.attr('ng-show', 
                    'form.' + scope.conf.field + '.$error.required');
                row.append(required);
            }

            $compile(row)(scope);
            element.append(row);
        }
    }
});

problem is that validation for field name doesn't work and validation text required is never shown. May be form in ng-show is unknown in directive. But I don't know how to pass form into field directive. Can you help me how to fix it? Thanks.

here is live code: http://plnkr.co/edit/j0xc7iV1Sqid2VK6rMDF?p=preview

like image 608
MarekLi Avatar asked May 03 '13 21:05

MarekLi


People also ask

Which method is used to add dynamic validation to the forms in angular?

The FormGroup class exposes an API that enables us to set validators dynamically. We need to listen to optionB value changes and based on that we add or remove the validators we require. We also call the control's updateValueAndValidity() method, as we need to recalculate the value and validation status of the control.

How do you add a validator to FormControl dynamically?

We can add Validators dynamically using the SetValidators or SetAsyncValidators. This method is available to FormControl, FormGroup & FormArray. There are many use cases where it is required to add/remove validators dynamically to a FormControl or FormGroup.


2 Answers

Todo:

before:

$compile(row)(scope);
element.append(row);

after:

element.append(row);
$compile(row)(scope);

p/s in 'planker' for facilities add css:

.ng-invalid {
  border: 1px solid red;
}
like image 176
Vladimir Avatar answered Sep 20 '22 14:09

Vladimir


You'll need to use ng-form directive and push the dynamic field directly into form object.

This thread can help you out: https://github.com/angular/angular.js/issues/1404

like image 38
Ygor Cardoso Avatar answered Sep 18 '22 14:09

Ygor Cardoso