Search code examples
angularangular2-templateangular2-forms

Validation of dynamic angular2 forms


I have a FormGroup with a template-driven validation. I need my form to have a section of dynamically generated inputs, created by ngFor. Unfortunately, angular2 now requires every input within FormGroup using ngModel to be named. How can I deal with it? Template-driven solution is preferred.

Form code is as follows:

<form #f="ngForm" (ngSubmit)="createProfile()">
    <div class="row align-items-center mb-2">
        <div class="col-sm-2">
            <b>Name:</b>
        </div>
        <div class="col-sm-10">
            <input type="text" [(ngModel)]="profile.name" class="form-control"
            name="profileName" required pattern="[a-zA-Z_][a-zA-Z_\-0-9]*">
        </div>
    </div>

    <div class="channelBox">
        <div *ngFor="let c of profile.channels">
            <div class="row align-items-center mb-2">
                <div class="col-sm-2">
                    <b>Name:</b>
                </div>
                <div class="col-sm-10">
                    <input type="text" [(ngModel)]="c.name" class="form-control" required
                    pattern="[a-zA-Z_][a-zA-Z_\-0-9]*" name="channelName">
                </div>
            </div>

            <div class="row align-items-center mb-2">
                <div class="col-sm-2">
                    <b>Filter:</b>
                </div>
                <div class="col-sm-10">
                    <textarea class="form-control" rows="4" [(ngModel)]="c.filter"
                    name="channelFilter">
                    </textarea>
                </div>
            </div>

            <div class="row align-items-center mb-2">
                <div class="col-sm-2">
                    <b>Sources:</b>
                </div>
                <div class="col-sm-10">
                    <label *ngFor="let s of c.sources">
                        <input type="checkbox" [(ngModel)]="s.checked" 
                        name="channelCheck">
                        {{ s.name }}
                    </label>
                </div>
            </div>
        </div>
    </div>

    <div>
        <button class="btn btn-success" type="submit" [disabled]="!f.valid">
            Create profile
        </button>
        <button class="btn btn-default" (click)="d('reason')">Cancel</button>
    </div>
</form>

Thanks in advance for help


Solution

  • I am guessing that you are looking for this: name="{{'someName'+i}}". It will indented with the loop index and some name, preferably something meaningful.

    You can also use [name]="'someName' + i" syntax.

    <div *ngFor="let c of profile.channels; let i=index">
                <div class="row align-items-center mb-2">
                    <div class="col-sm-2">
                        <b>Name:</b>
                    </div>
                    <div class="col-sm-10">
                        <input type="text" [(ngModel)]="c.name" class="form-control" required 
                         name="{{'someName'+i}}"
                        pattern="[a-zA-Z_][a-zA-Z_\-0-9]*" name="channelName">
                    </div>
                </div>
    ....