Dynamic form validation using ng-repeat is not working

249 Views Asked by At

I am doing a dynamic form validation as follows:

<form role="form" name="myForm">
    <div class="row" ng-repeat="field in fields">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="{{field.title}}" name="formData[field.name]" ng-model="formData[field.name]" required ng-minlength=5 ng-maxlength=20>
            <div class="error">
                <small class="error" ng-show="myForm.formData[field.name].$error.minlength">
                    {{field.minValidationMessage}}
                </small>
            </div>
        </div>
    </div>
    ...
</form>

my controller is as follows:

    $scope.fields = [{
    name: 'name',
    title: 'Name',
    placeholder: 'Enter product name',
    ...
    },{
    name: 'code',
    title: 'Code',
    placeholder: 'Enter product code',
    ...
    }]

    $scope.formData = {
    name: '',
    code: ''
 };

Here the following code is not working:

<small class="error" ng-show="myForm.formData[field.name].$error.minlength">
    {{field.minValidationMessage}}
</small>

However without ng-repeat(static) it is working as follows:

<small class="error" ng-show="myForm.code.$error.minlength">
    Your name is required to be at least 3 characters
</small>

How to refer myForm.formData[field.name] in ng-show.

Thanks in advance.

0

There are 0 best solutions below