import {TranslateComponent} from '@eg/share/translate/translate.component';
import {FmRecordEditorActionComponent} from './fm-editor-action.component';
import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
+import {Directive, HostBinding} from '@angular/core';
+import {AbstractControl, NG_VALIDATORS, ValidationErrors, Validator, Validators} from '@angular/forms';
interface CustomFieldTemplate {
template: TemplateRef<any>;
// help text to display via a popover
helpText?: StringComponent;
+
+ // minimum and maximum permitted values for int fields
+ min?: number;
+ max?: number;
}
@Component({
field.helpText.current().then(help => field.helpTextValue = help);
}
+ if (fieldOptions.min) {
+ field.min = Number(fieldOptions.min);
+ }
+ if (fieldOptions.max) {
+ field.max = Number(fieldOptions.max);
+ }
+
return promise || Promise.resolve();
}
}
}
+// https://stackoverflow.com/a/57812865
+@Directive({
+ selector: 'input[type=number][egMin][formControlName],input[type=number][egMin][formControl],input[type=number][egMin][ngModel]',
+ providers: [{ provide: NG_VALIDATORS, useExisting: MinValidatorDirective, multi: true }]
+})
+export class MinValidatorDirective implements Validator {
+ @HostBinding('attr.egMin') @Input() egMin: number;
+
+ constructor() { }
+
+ validate(control: AbstractControl): ValidationErrors | null {
+ const validator = Validators.min(this.egMin);
+ return validator(control);
+ }
+}
+@Directive({
+ selector: 'input[type=number][egMax][formControlName],input[type=number][egMax][formControl],input[type=number][egMax][ngModel]',
+ providers: [{ provide: NG_VALIDATORS, useExisting: MaxValidatorDirective, multi: true }]
+})
+export class MaxValidatorDirective implements Validator {
+ @HostBinding('attr.egMax') @Input() egMax: number;
+
+ constructor() { }
+
+ validate(control: AbstractControl): ValidationErrors | null {
+ const validator = Validators.max(this.egMax);
+ return validator(control);
+ }
+}
import {CommonWidgetsModule} from '@eg/share/common-widgets.module';
import {StringModule} from '@eg/share/string/string.module';
import {TranslateModule} from '@eg/share/translate/translate.module';
-import {FmRecordEditorComponent} from './fm-editor.component';
+import {FmRecordEditorComponent, MinValidatorDirective, MaxValidatorDirective} from './fm-editor.component';
import {FmRecordEditorActionComponent} from './fm-editor-action.component';
@NgModule({
declarations: [
FmRecordEditorComponent,
- FmRecordEditorActionComponent
+ FmRecordEditorActionComponent,
+ MinValidatorDirective,
+ MaxValidatorDirective
],
imports: [
EgCommonModule,