ngPattern problem with dynamic forms inside ngRepeat

180 Views Asked by At

I've been stuck on a problem for a while now and I feel I've exhausted all efforts.

I have a dynamic form created using an ng-repeat, for some reason ng-pattern will not work in this scenario, ng-required works perfect though.

field.label = "test input"
field.key = "test_input"
field.type = "text"
field.templateOptions.type = "text"
field.templateOptions.required = true
field.templateOptions.pattern = "\\d+"

Here is the code:

<form name="form">
   <div ng-repeat="field in fields">
      <div ng-if="field-type=='input'">
         <label>{{field.label}}</label>
         <input 
             type="{{field.templateOptions.type}}" 
             name="{{field.key}}"
             ng-required="{{field.templateOptions.required}}"
             ng-pattern="{{field.templateOptions.pattern}}"
             ng-model="model[field.key]"
         />
         <div ng-show="form[field.key].$error.required">Required Field</div>
         <div ng-show="form[field.key].$dirty && form[field.key].$error.pattern">Numeric Field Required</div>
      </div>
   </div>

I've even tried using a ng-form inside the ng-repeat (this didnt work).

This works, however its not in a ng-repeat:

<form name="form">
    <label>test input</label>
    <input type="text"
            name="test_input"
            ng-required="true"
            ng-pattern="\\d+"
            ng-model="model" />
    <div ng-show="form.test_input.$error.required">Required Field</div>
    <div ng-show="form.test_input.$dirty && form.test_input.$error.pattern">Numeric Field Required</div>
</form>

Any help will be greatly appreciated, thanks

0

There are 0 best solutions below