0

I have a ngRepeat populating a form but form.input.$error.pattern is true (the input does not match the RegExp) with anything in the text input for the first input. For all inputs after that form.input.$error.required stays true, even with something present in the input text. The classes .ng-pristine, .ng-touched, .ng-dirty, etc. still behave as expected.

A service defines the regular expressions:

.service('Apform', function () {
  'use strict';
var patt = {
            alpha: /^[a-zA-Z]+$/,
            alpha_numeric: /^[a-zA-Z0-9]+$/,
            phone: /^\d{3}[\-]\d{3}[\-]\d{4}$/,
            postal: /^[A-Za-z][0-9][A-Za-z] [0-9][A-Za-z][0-9]$/,
        };
return patt;

Which is injected into the controller:

controller: function ($scope, Apform) {

        $scope.patt = Apform;
        $scope.application = [
            {
                label: 'Name',
                model: 'clientName',
                required: true,
                pattern: $scope.patt.alpha,
                error: 'Valid with letters only',
                type: 'text',
            },
            {
                label: 'Address',
                model: 'clientAddress',
                required: true,
                pattern: $scope.patt.alpha_numeric,
                error: 'Valid with letters and numbers only',
                type: 'text',
            },
    ....
    ];}};});

The object is used in an ngRepeat inside ngForm:

<ng-form name="applicationForm">
        <div ng-repeat="form in application" class="row">
            <div ng-if="form.type==='text'
                 class="small-8">
                <div class="row input-wrapper">
                    <div class="small-8">
                        <label for="{{form.label}}"
                               class="left inline">{{form.label}}
                        </label>
                    </div>
                    <div class="small-3 colums">
                        <input type="{{form.type}}"
                               id="{{form.label}}"
                               name="{{form.label}}"
                               placeholder="{{form.label}}"
                               ng-required="{{form.required}}"
                               ng-pattern="'{{form.pattern}}'"
                               ng-model="$parent.$parent.output[form.model]">
                    </div>
                    <small>
                        applicationForm[{{form.label}}].$valid: {{applicationForm[form.label].$valid}}<br>
                        applicationForm[{{form.label}}].$error: {{applicationForm[form.label].$error}}<br>
                        applicationForm[{{form.label}}].required: {{applicationForm[form.label].required}}<br>
                        applicationForm.$valid: {{applicationForm.$valid}}
                    </small>
                </div>
            </div>
</ng-form>

What's going wrong?

ninjabugs
  • 103
  • 7

0 Answers0