Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cb2-12600): add form control to specialist contingency test #1500

Merged
merged 6 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/forms/services/dynamic-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export class DynamicFormService {
[ValidatorNames.PastDate]: () => CustomValidators.pastDate,
[ValidatorNames.Pattern]: (args: string) => Validators.pattern(args),
[ValidatorNames.Required]: () => Validators.required,
[ValidatorNames.RequiredIfEquals]: (args: { sibling: string; value: unknown[] }) => CustomValidators.requiredIfEquals(args.sibling, args.value),
[ValidatorNames.RequiredIfEquals]: (args: { sibling: string; value: unknown[], customErrorMessage?: string }) =>
CustomValidators.requiredIfEquals(args.sibling, args.value, args.customErrorMessage),
[ValidatorNames.requiredIfAllEquals]: (args: { sibling: string; value: unknown[] }) =>
CustomValidators.requiredIfAllEquals(args.sibling, args.value),
[ValidatorNames.RequiredIfNotEquals]: (args: { sibling: string; value: unknown[] }) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ export const ContingencyTestSectionSpecialistGroup1: FormNode = {
{ value: 'fail', label: 'Fail' },
{ value: 'prs', label: 'PRS' },
],
validators: [
{
name: ValidatorNames.ShowGroupsWhenIncludes,
args: {
values: ['fail'],
groups: ['failOnly'],
},
},
{
name: ValidatorNames.HideGroupsWhenExcludes,
args: {
values: ['fail'],
groups: ['failOnly'],
},
},
],
asyncValidators: [
{ name: AsyncValidatorNames.ResultDependantOnRequiredStandards },
{
Expand Down Expand Up @@ -151,6 +167,26 @@ export const ContingencyTestSectionSpecialistGroup1: FormNode = {
],
validators: [{ name: ValidatorNames.Required }],
},
{
name: 'reapplicationDate',
label: 'Reapplication date',
hint: 'For example, 27 3 2007',
editType: FormNodeEditTypes.DATE,
viewType: FormNodeViewTypes.DATE,
type: FormNodeTypes.CONTROL,
groups: ['failOnly'],
validators: [
{
name: ValidatorNames.RequiredIfEquals,
args: {
sibling: 'testResult',
value: ['fail'],
customErrorMessage: 'Reapplication date is required',
},
},
{ name: ValidatorNames.FutureDate },
],
},
],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ export const ContingencyTestSectionSpecialistGroup5: FormNode = {
{ value: 'fail', label: 'Fail' },
{ value: 'prs', label: 'PRS' },
],
validators: [
{
name: ValidatorNames.ShowGroupsWhenIncludes,
args: {
values: ['fail'],
groups: ['failOnly'],
},
},
{
name: ValidatorNames.HideGroupsWhenExcludes,
args: {
values: ['fail'],
groups: ['failOnly'],
},
},
],
asyncValidators: [
{ name: AsyncValidatorNames.ResultDependantOnRequiredStandards },
{
Expand Down Expand Up @@ -151,6 +167,26 @@ export const ContingencyTestSectionSpecialistGroup5: FormNode = {
],
validators: [{ name: ValidatorNames.Required }],
},
{
name: 'reapplicationDate',
label: 'Reapplication date',
hint: 'For example, 27 3 2007',
editType: FormNodeEditTypes.DATE,
viewType: FormNodeViewTypes.DATE,
type: FormNodeTypes.CONTROL,
groups: ['failOnly'],
validators: [
{
name: ValidatorNames.RequiredIfEquals,
args: {
sibling: 'testResult',
value: ['fail'],
customErrorMessage: 'Reapplication date is required',
},
},
{ name: ValidatorNames.FutureDate },
],
},
],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,13 @@ export const SpecialistTestSectionGroup1: FormNode = {
label: 'Certificate number',
type: FormNodeTypes.CONTROL,
editType: FormNodeEditTypes.TEXT,
validators: [{ name: ValidatorNames.Alphanumeric },
validators: [
{ name: ValidatorNames.Alphanumeric },
{
name: ValidatorNames.RequiredIfEquals,
args: {
sibling: 'testResult',
value: [
'pass',
'prs',
],
value: ['pass', 'prs'],
},
},
],
Expand Down Expand Up @@ -180,6 +178,26 @@ export const SpecialistTestSectionGroup1: FormNode = {
],
validators: [{ name: ValidatorNames.Required }],
},
{
name: 'reapplicationDate',
label: 'Reapplication date',
hint: 'For example, 27 3 2007',
editType: FormNodeEditTypes.DATE,
viewType: FormNodeViewTypes.DATE,
type: FormNodeTypes.CONTROL,
groups: ['failOnly'],
validators: [
{
name: ValidatorNames.RequiredIfEquals,
args: {
sibling: 'testResult',
value: ['fail'],
customErrorMessage: 'Reapplication date is required',
},
},
{ name: ValidatorNames.FutureDate },
],
},
],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export const SpecialistTestSectionGroup5: FormNode = {
{ name: ValidatorNames.HideIfNotEqual, args: { sibling: 'reasonForAbandoning', value: 'abandoned' } },
{ name: ValidatorNames.HideIfNotEqual, args: { sibling: 'additionalCommentsForAbandon', value: 'abandoned' } },
],
asyncValidators: [{ name: AsyncValidatorNames.ResultDependantOnRequiredStandards },
asyncValidators: [
{ name: AsyncValidatorNames.ResultDependantOnRequiredStandards },
{
name: AsyncValidatorNames.HideIfEqualsWithCondition,
args: {
Expand Down Expand Up @@ -132,10 +133,7 @@ export const SpecialistTestSectionGroup5: FormNode = {
name: ValidatorNames.RequiredIfEquals,
args: {
sibling: 'testResult',
value: [
'pass',
'prs',
],
value: ['pass', 'prs'],
},
},
],
Expand Down Expand Up @@ -179,6 +177,26 @@ export const SpecialistTestSectionGroup5: FormNode = {
],
validators: [{ name: ValidatorNames.Required }],
},
{
name: 'reapplicationDate',
label: 'Reapplication date',
hint: 'For example, 27 3 2007',
editType: FormNodeEditTypes.DATE,
viewType: FormNodeViewTypes.DATE,
type: FormNodeTypes.CONTROL,
groups: ['failOnly'],
validators: [
{
name: ValidatorNames.RequiredIfEquals,
args: {
sibling: 'testResult',
value: ['fail'],
customErrorMessage: 'Reapplication date is required',
},
},
{ name: ValidatorNames.FutureDate },
],
},
],
},
],
Expand Down
3 changes: 2 additions & 1 deletion src/app/forms/utils/error-message-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const ErrorMessageMap: Record<string, Function> = {
[ValidatorNames.PastDate]: (err: boolean, label?: string) => `${label || 'This date'} must be in the past`,
[ValidatorNames.Pattern]: (err: boolean, label?: string) => `${label || DEFAULT_LABEL} must match a pattern`,
[ValidatorNames.Required]: (err: boolean, label?: string) => `${label || DEFAULT_LABEL} is required`,
[ValidatorNames.RequiredIfEquals]: (err: { sibling: string }, label?: string) => `${label || DEFAULT_LABEL} is required with ${err.sibling}`,
[ValidatorNames.RequiredIfEquals]: (err: { sibling: string; customErrorMessage: string }, label?: string) =>
err.customErrorMessage ?? `${label || DEFAULT_LABEL} is required with ${err.sibling}`,
[ValidatorNames.RequiredIfNotEquals]: (err: { sibling: string }, label?: string) => `${label || DEFAULT_LABEL} is required with ${err.sibling}`,
[ValidatorNames.requiredIfAllEquals]: (err: { sibling: string }, label?: string) => `${label || DEFAULT_LABEL} is required with ${err.sibling}`,
[ValidatorNames.RequiredIfNotHidden]: (label?: string) => `${label || DEFAULT_LABEL} is required`,
Expand Down
4 changes: 2 additions & 2 deletions src/app/forms/validators/custom-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class CustomValidators {
return null;
};

static requiredIfEquals = (sibling: string, values: unknown[]): ValidatorFn =>
static requiredIfEquals = (sibling: string, values: unknown[], customErrorMessage?: string): ValidatorFn =>
(control: AbstractControl): ValidationErrors | null => {
if (!control?.parent) return null;

Expand All @@ -126,7 +126,7 @@ export class CustomValidators {
|| (Array.isArray(control.value) && (control.value.length === 0 || control.value.every((val) => !val)));

return isSiblingValueIncluded && isControlValueEmpty && isSiblingVisible
? { requiredIfEquals: { sibling: siblingControl.meta.label } }
? { requiredIfEquals: { sibling: siblingControl.meta.label, customErrorMessage } }
: null;
};

Expand Down
Loading