Skip to content

Commit

Permalink
Fix form validation initialisation problem
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusyerli1 authored and clemente-raposo committed Jul 20, 2023
1 parent 09c6895 commit fd0ffa6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* the words "Supercharged by SuiteCRM".
*/

import {Component, HostBinding, Input, OnInit, Type, OnChanges, SimpleChanges} from '@angular/core';
import {Component, HostBinding, Input, OnInit, Type} from '@angular/core';
import {EDITABLE_VIEW_MODES, Field, Record, StringMap, ViewMode} from 'common';
import {Router} from '@angular/router';
import {ModuleNameMapper} from '../../services/navigation/module-name-mapper/module-name-mapper.service';
Expand All @@ -39,7 +39,7 @@ import {
templateUrl: './dynamic-field.component.html',
styleUrls: []
})
export class DynamicFieldComponent implements OnInit, OnChanges {
export class DynamicFieldComponent implements OnInit {

@Input('mode') mode: string;
@Input('type') type: string;
Expand Down Expand Up @@ -82,47 +82,6 @@ export class DynamicFieldComponent implements OnInit, OnChanges {
this.setHostClass();
}

ngOnChanges(changes: SimpleChanges): void {

if (!changes.mode) {
return;
}

if (this.isEdit()) {
this.initValidators();
} else {
this.resetValidators();
}

}

protected initValidators(): void {

const formControl = this.field?.formControl ?? null;

if (!formControl) {
return;
}

this.resetValidators();

const validators = this.field?.validators ?? [];
const asyncValidators = this.field?.asyncValidators ?? [];

if (validators.length) {
this.field?.formControl?.setValidators(validators);
}
if (asyncValidators.length) {
this.field?.formControl?.setAsyncValidators(asyncValidators);
}

}

resetValidators() {
this.field?.formControl?.clearValidators();
this.field?.formControl?.clearAsyncValidators();
}

isLink(): boolean {

if (EDITABLE_VIEW_MODES.includes(this.mode as ViewMode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,17 @@ export class RecordContentAdapter implements RecordContentDataSource {
}

getRecord(): Observable<Record> {
return this.store.stagingRecord$;
return combineLatest([this.store.stagingRecord$, this.store.mode$]).pipe(
map(([record, mode]) => {
if (mode === 'edit' || mode === 'create') {
this.store.initValidators(record);
} else {
this.store.resetValidatorsForAllFields(record);
}
record.formGroup.enable();
return record;
})
);
}

protected getLayout(recordMeta: RecordViewMetadata): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,53 @@ export class RecordViewStore extends ViewStore implements StateStore {
protected loadPreference(module: string, storageKey: string): any {
return this.preferences.getUi(module, this.getPreferenceKey(storageKey));
}

initValidators(record: Record): void {
if(!record || !Object.keys(record?.fields).length) {
return;
}

Object.keys(record.fields).forEach(fieldName => {
const field = record.fields[fieldName];
const formControl = field?.formControl ?? null;
if (!formControl) {
return;
}

this.resetValidators(field);

const validators = field?.validators ?? [];
const asyncValidators = field?.asyncValidators ?? [];

if (validators.length) {
field?.formControl?.setValidators(validators);
}
if (asyncValidators.length) {
field?.formControl?.setAsyncValidators(asyncValidators);
}
});

}

resetValidators(field) {
field?.formControl?.clearValidators();
field?.formControl?.clearAsyncValidators();
}

resetValidatorsForAllFields(record) {
if(!record || !record?.fields?.length) {
return ;
}
Object.keys(record.fields).forEach(fieldName => {
const field = record.fields[fieldName];
const formControl = field?.formControl ?? null;

if (!formControl) {
return;
}

this.resetValidators(field);
});
}

}

0 comments on commit fd0ffa6

Please sign in to comment.