diff --git a/src/app/draftpoll/draftpoll.page.ts b/src/app/draftpoll/draftpoll.page.ts index 6e77959ee..20f65cb5f 100644 --- a/src/app/draftpoll/draftpoll.page.ts +++ b/src/app/draftpoll/draftpoll.page.ts @@ -41,7 +41,7 @@ import { environment } from 'src/environments/environment'; import { unique_name_validator$ } from '../sharedcomponents/unique-form-validator'; import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators' +import { map, startWith } from 'rxjs/operators' type option_data_t = { oid?, name?, desc?, url?, ratings? }; @@ -863,6 +863,7 @@ export class DraftpollPage implements OnInit { existingOptionName$(currentControlName: string): Observable { return this.formGroup.valueChanges.pipe( + startWith({}), map(values => { const optionNameKeys = Object.keys(values as Object).filter(k => { return k.includes('option_name') && k !== currentControlName }) const existingOptionNames: string[] = []; diff --git a/src/app/sharedcomponents/unique-form-validator.ts b/src/app/sharedcomponents/unique-form-validator.ts index 8d3e985de..7e92cb619 100644 --- a/src/app/sharedcomponents/unique-form-validator.ts +++ b/src/app/sharedcomponents/unique-form-validator.ts @@ -1,16 +1,26 @@ -import { AbstractControl, ValidationErrors, AsyncValidatorFn } from '@angular/forms'; -import { Observable } from 'rxjs'; -import { map, take } from 'rxjs/operators' +import { + AbstractControl, + ValidationErrors, + AsyncValidatorFn, +} from "@angular/forms"; +import { Observable, of } from "rxjs"; +import { map, take } from "rxjs/operators"; /** The option-name has to be unique */ -export function unique_name_validator$(value$: Observable): AsyncValidatorFn { +export function unique_name_validator$( + value$: Observable +): AsyncValidatorFn { return (control: AbstractControl): Observable => { + if (!control.valueChanges || control.pristine) { + return of(null); + } + return value$.pipe( - map(names => { + map((names) => { return names.includes(control.value) ? { not_unique: true } : null; }), take(1) - ) + ); }; }