Skip to content

Commit dc6a21c

Browse files
committed
docs: add array controls validation example
1 parent 954d5ed commit dc6a21c

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/app/app.module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ export function httpLoaderFactory(http: HttpClient): TranslateLoader {
3030
FormsModule,
3131
ReactiveFormsModule,
3232
NgxValidationErrorsModule.forRoot({
33-
defaultContext: 'CUSTOM_GENERAL',
34-
errorComponent: CustomErrorsComponent as any
3533
}),
3634
TranslateModule.forRoot({
3735
loader: {

src/app/lazy/lazy-form/lazy-form.component.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ <h2 style="text-align: center">xtream validation errors lazy</h2>
1212
<label>Surmane</label>
1313
<input class="form-control" formControlName="surname"/>
1414
</div>
15+
<div formArrayContainer>
16+
<div formArrayName="checkBoxes" class="form-group">
17+
<div *ngFor="let check of boxesInfo; let i = index">
18+
<input type="checkbox" id="check_{{i}}" [formControlName]="i">
19+
<label for="check_{{i}}" id="label_check_{{i}}">{{boxesInfo[i]}}</label>
20+
</div>
21+
</div>
22+
</div>
23+
1524
<button class="btn btn-primary" [disabled]="heroForm.invalid">Submit</button>
1625

1726
</form>

src/app/lazy/lazy-form/lazy-form.component.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import {Component, OnInit} from '@angular/core';
2-
import {FormControl, FormGroup, Validators} from '@angular/forms';
2+
import {AbstractControl, FormArray, FormControl, FormGroup, Validators} from '@angular/forms';
33
import {TranslateService} from '@ngx-translate/core';
44

5+
function minCheckSelected(size: number) {
6+
return (control: AbstractControl) => {
7+
const values = control.value as (boolean | undefined)[];
8+
const selected = values.filter(v => !!v).length;
9+
if (selected < size) {
10+
return {checkBoxes: true};
11+
}
12+
return null;
13+
};
14+
}
15+
516
@Component({
617
selector: 'app-lazy-form',
718
templateUrl: './lazy-form.component.html',
@@ -11,10 +22,18 @@ export class LazyFormComponent implements OnInit {
1122

1223
heroForm: FormGroup;
1324

25+
boxesInfo = [
26+
'a',
27+
'b',
28+
'c',
29+
'd'
30+
];
31+
1432
constructor(private translateService: TranslateService) {
1533
this.heroForm = new FormGroup({
1634
name: new FormControl(null, [Validators.required, Validators.minLength(4)]),
17-
surname: new FormControl(null, [Validators.required, Validators.maxLength(1000)])
35+
surname: new FormControl(null, [Validators.required, Validators.maxLength(1000)]),
36+
checkBoxes: new FormArray(this.boxesInfo.map(a => new FormControl()), [minCheckSelected(1)])
1837
});
1938
}
2039

src/assets/i18n/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
},
55
"GENERAL": {
66
"ERRORS": {
7-
"MINLENGTH": "The field it too short. You need ti insert at least {{requiredLength}}"
7+
"MINLENGTH": "The field it too short. You need ti insert at least {{requiredLength}}",
8+
"REQUIRED" : "The field is required"
89
}
910
},
1011
"NEW_HERO": {

0 commit comments

Comments
 (0)