Skip to content

Commit

Permalink
Submission form now displays custom messages for regex validated fiel…
Browse files Browse the repository at this point in the history
…ds if they exist
  • Loading branch information
max-nuding committed Nov 15, 2023
1 parent 404ccd9 commit 038e31c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/app/shared/form/builder/parsers/concat-field-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SUBMISSION_ID
} from './field-parser';
import { DsDynamicInputModel, DsDynamicInputModelConfig } from '../ds-dynamic-form-ui/models/ds-dynamic-input.model';
import { TranslateService } from '@ngx-translate/core';

export class ConcatFieldParser extends FieldParser {

Expand All @@ -27,10 +28,11 @@ export class ConcatFieldParser extends FieldParser {
@Inject(CONFIG_DATA) configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) initFormValues,
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions,
translate: TranslateService,
protected separator: string,
protected firstPlaceholder: string = null,
protected secondPlaceholder: string = null) {
super(submissionId, configData, initFormValues, parserOptions);
super(submissionId, configData, initFormValues, parserOptions, translate);

this.separator = separator;
this.firstPlaceholder = firstPlaceholder;
Expand Down
4 changes: 3 additions & 1 deletion src/app/shared/form/builder/parsers/dropdown-field-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { isNotEmpty } from '../../../empty.util';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
import { ParserOptions } from './parser-options';
import { TranslateService } from '@ngx-translate/core';

export class DropdownFieldParser extends FieldParser {

Expand All @@ -23,8 +24,9 @@ export class DropdownFieldParser extends FieldParser {
@Inject(CONFIG_DATA) configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) initFormValues,
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions,
translate: TranslateService
) {
super(submissionId, configData, initFormValues, parserOptions);
super(submissionId, configData, initFormValues, parserOptions, translate);
}

public modelFactory(fieldValue?: FormFieldMetadataValueObject | any, label?: boolean): any {
Expand Down
9 changes: 7 additions & 2 deletions src/app/shared/form/builder/parsers/field-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { VocabularyOptions } from '../../../../core/submission/vocabularies/mode
import { ParserType } from './parser-type';
import { isNgbDateStruct } from '../../../date.util';
import { SubmissionScopeType } from '../../../../core/submission/submission-scope-type';
import { TranslateService } from '@ngx-translate/core';

export const SUBMISSION_ID: InjectionToken<string> = new InjectionToken<string>('submissionId');
export const CONFIG_DATA: InjectionToken<FormFieldModel> = new InjectionToken<FormFieldModel>('configData');
Expand All @@ -50,7 +51,8 @@ export abstract class FieldParser {
@Inject(SUBMISSION_ID) protected submissionId: string,
@Inject(CONFIG_DATA) protected configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) protected initFormValues: any,
@Inject(PARSER_OPTIONS) protected parserOptions: ParserOptions
@Inject(PARSER_OPTIONS) protected parserOptions: ParserOptions,
protected translate: TranslateService
) {
}

Expand Down Expand Up @@ -395,11 +397,14 @@ export abstract class FieldParser {
} else {
regex = new RegExp(this.configData.input.regex);
}
const baseTranslationKey = 'error.validation.pattern';
const fieldranslationKey = `${baseTranslationKey}.${controlModel.id}`;
const fieldTranslationExists = this.translate.instant(fieldranslationKey) !== fieldranslationKey;
controlModel.validators = Object.assign({}, controlModel.validators, { pattern: regex });
controlModel.errorMessages = Object.assign(
{},
controlModel.errorMessages,
{ pattern: 'error.validation.pattern' });
{ pattern: fieldTranslationExists ? fieldranslationKey : baseTranslationKey });
}

protected markAsRequired(controlModel) {
Expand Down
6 changes: 4 additions & 2 deletions src/app/shared/form/builder/parsers/name-field-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { FormFieldModel } from '../models/form-field.model';
import { ConcatFieldParser } from './concat-field-parser';
import { CONFIG_DATA, INIT_FORM_VALUES, PARSER_OPTIONS, SUBMISSION_ID } from './field-parser';
Expand All @@ -10,8 +11,9 @@ export class NameFieldParser extends ConcatFieldParser {
@Inject(SUBMISSION_ID) submissionId: string,
@Inject(CONFIG_DATA) configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) initFormValues,
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions,
translate: TranslateService
) {
super(submissionId, configData, initFormValues, parserOptions, ',', 'form.last-name', 'form.first-name');
super(submissionId, configData, initFormValues, parserOptions, translate, ',', 'form.last-name', 'form.first-name');
}
}
2 changes: 2 additions & 0 deletions src/app/shared/form/builder/parsers/parser-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import { SeriesFieldParser } from './series-field-parser';
import { TagFieldParser } from './tag-field-parser';
import { TextareaFieldParser } from './textarea-field-parser';
import { DisabledFieldParser } from './disabled-field-parser';
import { TranslateService } from '@ngx-translate/core';

const fieldParserDeps = [
SUBMISSION_ID,
CONFIG_DATA,
INIT_FORM_VALUES,
PARSER_OPTIONS,
TranslateService
];

/**
Expand Down
6 changes: 4 additions & 2 deletions src/app/shared/form/builder/parsers/series-field-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { FormFieldModel } from '../models/form-field.model';
import { ConcatFieldParser } from './concat-field-parser';
import { CONFIG_DATA, INIT_FORM_VALUES, PARSER_OPTIONS, SUBMISSION_ID } from './field-parser';
Expand All @@ -10,8 +11,9 @@ export class SeriesFieldParser extends ConcatFieldParser {
@Inject(SUBMISSION_ID) submissionId: string,
@Inject(CONFIG_DATA) configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) initFormValues,
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions,
translate: TranslateService
) {
super(submissionId, configData, initFormValues, parserOptions, ';');
super(submissionId, configData, initFormValues, parserOptions, translate, ';');
}
}

0 comments on commit 038e31c

Please sign in to comment.