Commit c56e4b82 authored by Florent POITTEVIN's avatar Florent POITTEVIN
Browse files

feat: multiple validation on metadata type

parent a5063e8e
......@@ -11,6 +11,7 @@
autofocus
[solidifyFocusFirstElement]="true"
[formControl]="fd"
(blur)="checkAvailableMultiple(formDefinition.name, formDefinition.version)"
[required]="formValidationHelper.hasRequiredField(fd)"
>
<mat-error #errors></mat-error>
......@@ -57,6 +58,7 @@
<input matInput
[formControl]="fd"
[solidifyValidation]="errors"
(blur)="checkAvailableMultiple(formDefinition.name, formDefinition.version)"
[required]="formValidationHelper.hasRequiredField(fd)"
>
<mat-error #errors></mat-error>
......@@ -123,7 +125,7 @@
color="primary"
type="submit"
(keydown.enter)="onSubmit()"
[disabled]="!(this.uploadedOrChangedFile && form.valid || this.isEditAction && form.valid && form.dirty) && !(this.uploadedOrChangedFile && !this.isEditAction && form.valid )"
[disabled]="form.invalid || (form.pristine && !this.uploadedOrChangedFile)"
>
{{'admin.metadataType.form.submit' | translate }}
</button>
......
......@@ -3,7 +3,6 @@ import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Input,
Output,
} from "@angular/core";
import {
......@@ -40,16 +39,12 @@ export class AdminMetadataTypeFormPresentational extends SharedAbstractFormPrese
readonly maxSizeFile: number = 25000000;
uploadedOrChangedFile: boolean = false;
@Input()
isEditAction: boolean = false;
metadataFormatEnum: KeyValue[] = MetadataTypeFormatEnumHelper.getListKeyValue();
private readonly _fileUploadBS: BehaviorSubject<File | undefined> = new BehaviorSubject<File | undefined>(undefined);
@Output("fileUploadChange")
readonly fileUploadObs: Observable<File | undefined> = ObservableUtil.asObservable(this._fileUploadBS);
constructor(protected readonly _changeDetectorRef: ChangeDetectorRef,
private readonly _notificationService: NotificationService,
private readonly _fb: FormBuilder) {
......@@ -112,7 +107,6 @@ export class AdminMetadataTypeFormPresentational extends SharedAbstractFormPrese
this._fileUploadBS.next(file);
}
isFileAccepted(file: File): boolean {
if (file.type.startsWith("text") || file.type === "application/json" || file.name.endsWith(".xsd")) {
return true;
......
......@@ -151,4 +151,8 @@ export abstract class SharedAbstractFormPresentational<TResource extends BaseRes
checkAvailable(key: string, formControl: FormControl): void {
this._checkAvailableBS.next({key: key, formControl: formControl, changeDetector: this._changeDetectorRef});
}
checkAvailableMultiple(...keys: string[]): void {
this._checkAvailableBS.next({keys: keys, form: this.form, changeDetector: this._changeDetectorRef});
}
}
......@@ -21,7 +21,8 @@ import {
} from "solidify-frontend";
export class CrudHelper {
static checkAvailable(formControlKey: FormControlKey, store: Store, actions$: Actions, resourceNameSpace: ResourceNameSpace, resId: string | undefined): Observable<boolean> {
private static checkSingleConstraintAvailable(formControlKey: FormControlKey, store: Store, actions$: Actions, resourceNameSpace: ResourceNameSpace, resId: string | undefined): Observable<boolean> {
const queryParameters = new QueryParameters();
MappingObjectUtil.set(queryParameters.search.searchItems, formControlKey.key, formControlKey.formControl.value);
store.dispatch(ResourceActionHelper.getAll(resourceNameSpace, queryParameters));
......@@ -45,4 +46,55 @@ export class CrudHelper {
}),
);
}
private static checkMultipleConstraintAvailable(formControlKey: FormControlKey, store: Store, actions$: Actions, resourceNameSpace: ResourceNameSpace, resId: string | undefined): Observable<boolean> {
const queryParameters = new QueryParameters();
const keys = formControlKey.keys;
keys.forEach(k => {
MappingObjectUtil.set(queryParameters.search.searchItems, k, formControlKey.form.get(k).value);
});
store.dispatch(ResourceActionHelper.getAll(resourceNameSpace, queryParameters));
return actions$.pipe(ofActionCompleted(resourceNameSpace.GetAllSuccess))
.pipe(
take(1),
map(result => {
if (result.result.successful && result.action.list._data.length > 0) {
let list = result.action.list._data.filter((f: BaseResource) => isNullOrUndefined(resId) || f.resId !== resId);
keys.forEach(k => {
const value = formControlKey.form.get(k).value;
list = list.filter(t => (StringUtil.stringEmpty + t[k]).toLowerCase() === (StringUtil.stringEmpty + value).toLowerCase());
});
if (list.length === 0) {
return true;
}
keys.forEach(k => {
const formControl = formControlKey.form.get(k);
formControl.setErrors({
errorsFromBackend: [FormErrorEnum.ALREADY_USED],
});
});
formControlKey.form.markAsTouched();
formControlKey.changeDetector.detectChanges();
return false;
} else {
keys.forEach(k => {
const formControl = formControlKey.form.get(k);
formControl.updateValueAndValidity();
});
formControlKey.form.markAsTouched();
formControlKey.changeDetector.detectChanges();
}
}),
);
}
static checkAvailable(formControlKey: FormControlKey, store: Store, actions$: Actions, resourceNameSpace: ResourceNameSpace, resId: string | undefined): Observable<boolean> {
if (!isNullOrUndefined(formControlKey.keys) && !isNullOrUndefined(formControlKey.form)) {
return this.checkMultipleConstraintAvailable(formControlKey, store, actions$, resourceNameSpace, resId);
} else if (!isNullOrUndefined(formControlKey.key) && !isNullOrUndefined(formControlKey.formControl)) {
return this.checkSingleConstraintAvailable(formControlKey, store, actions$, resourceNameSpace, resId);
}
throw new Error("Should provide key and formControl for single constraint or key and form for multiple constraint");
}
}
import {ChangeDetectorRef} from "@angular/core";
import {FormControl} from "@angular/forms";
import {
FormControl,
FormGroup,
} from "@angular/forms";
export interface FormControlKey {
formControl: FormControl;
key: string;
key?: string;
formControl?: FormControl;
keys?: string[];
form?: FormGroup;
changeDetector: ChangeDetectorRef;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment