Validators
} from '@angular/forms';
-import { I18n } from '@ngx-translate/i18n-polyfill';
-import * as _ from 'lodash';
+import _ from 'lodash';
import { Observable, of as observableOf, timer as observableTimer } from 'rxjs';
import { map, switchMapTo, take } from 'rxjs/operators';
static unique(
serviceFn: existsServiceFn,
serviceFnThis: any = null,
+ usernameFn?: Function,
+ uidfield = false,
dueTime = 500
): AsyncValidatorFn {
+ let uname: string;
return (control: AbstractControl): Observable<ValidationErrors | null> => {
// Exit immediately if user has not interacted with the control yet
// or the control value is empty.
if (control.pristine || isEmptyInputValue(control.value)) {
return observableOf(null);
}
+ uname = control.value;
+ if (_.isFunction(usernameFn) && usernameFn() !== null && usernameFn() !== '') {
+ if (uidfield) {
+ uname = `${control.value}$${usernameFn()}`;
+ } else {
+ uname = `${usernameFn()}$${control.value}`;
+ }
+ }
+
// Forgot previous requests if a new one arrives within the specified
// delay time.
return observableTimer(dueTime).pipe(
- switchMapTo(serviceFn.call(serviceFnThis, control.value)),
+ switchMapTo(serviceFn.call(serviceFnThis, uname)),
map((resp: boolean) => {
if (!resp) {
return null;
* be called in a static one.
*/
static binaryMin(bytes: number): ValidatorFn {
- return (control: AbstractControl): { [key: string]: (i18n: I18n) => string } | null => {
+ return (control: AbstractControl): { [key: string]: () => string } | null => {
const formatterService = new FormatterService();
const currentBytes = new FormatterService().toBytes(control.value);
if (bytes <= currentBytes) {
}
const value = new DimlessBinaryPipe(formatterService).transform(bytes);
return {
- binaryMin: (i18n: I18n) => i18n(`Size has to be at least {{value}} or more`, { value })
+ binaryMin: () => $localize`Size has to be at least ${value} or more`
};
};
}
* be called in a static one.
*/
static binaryMax(bytes: number): ValidatorFn {
- return (control: AbstractControl): { [key: string]: (i18n: I18n) => string } | null => {
+ return (control: AbstractControl): { [key: string]: () => string } | null => {
const formatterService = new FormatterService();
const currentBytes = formatterService.toBytes(control.value);
if (bytes >= currentBytes) {
}
const value = new DimlessBinaryPipe(formatterService).transform(bytes);
return {
- binaryMax: (i18n: I18n) => i18n(`Size has to be at most {{value}} or less`, { value })
+ binaryMax: () => $localize`Size has to be at most ${value} or less`
};
};
}