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

Add custom required validation text #1272

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 schemas/json/layout/layout.schema.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"examples": [
{
"title": "some.text.binding",
"help": "some.other.text.binding"
"help": "some.other.text.binding",
"requiredValidation": "some.custom.required.validation.message"
}
]
},
Expand Down
32 changes: 22 additions & 10 deletions src/utils/validation/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ describe('utils > validation', () => {
id: 'c4Title',
value: 'component_4',
},
{
id: 'c4RequiredValidation',
value: 'Component_4 feltet er påkrevd og må besvares',
},
{
id: 'c5Title',
value: 'component_5',
Expand Down Expand Up @@ -170,6 +174,7 @@ describe('utils > validation', () => {
readOnly: false,
textResourceBindings: {
title: 'c4Title',
requiredValidation: 'c4RequiredValidation',
},
};

Expand Down Expand Up @@ -915,7 +920,7 @@ describe('utils > validation', () => {
},
'componentId_4-0': {
simpleBinding: {
errors: ['Du må fylle ut component_4'],
errors: ['Component_4 feltet er påkrevd og må besvares'],
warnings: [],
},
},
Expand Down Expand Up @@ -1048,12 +1053,19 @@ describe('utils > validation', () => {
const requiredFieldInSimpleGroup = 'required_in_group_simple';
const requiredError = (name?: string) => {
const fieldName = name || 'dette feltet';
return {
simpleBinding: {
errors: [`Du må fylle ut ${fieldName}`],
warnings: [],
},
};
return name == 'component_4'
? {
simpleBinding: {
errors: [`Component_4 feltet er påkrevd og må besvares`],
warnings: [],
},
}
: {
simpleBinding: {
errors: [`Du må fylle ut ${fieldName}`],
warnings: [],
},
};
};

it('should pass validation on required field in hidden group', () => {
Expand Down Expand Up @@ -1708,7 +1720,7 @@ describe('utils > validation', () => {
FormLayout: {
'componentId_4-0': {
simpleBinding: {
errors: ['Du må fylle ut component_4', 'Feil format eller verdi'],
errors: ['Component_4 feltet er påkrevd og må besvares', 'Feil format eller verdi'],
},
},
'componentId_5-0-1': {
Expand Down Expand Up @@ -1801,7 +1813,7 @@ describe('utils > validation', () => {
FormLayout: {
'componentId_4-0': {
simpleBinding: {
errors: ['Du må fylle ut component_4', 'Feil format eller verdi'],
errors: ['Component_4 feltet er påkrevd og må besvares', 'Feil format eller verdi'],
},
},
'componentId_5-0-1': {
Expand All @@ -1823,7 +1835,7 @@ describe('utils > validation', () => {
FormLayout: {
'componentId_4-1': {
simpleBinding: {
errors: ['Du må fylle ut component_4', 'Feil format eller verdi'],
errors: ['Component_4 feltet er påkrevd og må besvares', 'Feil format eller verdi'],
},
},
'componentId_5-1-0': {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ export function validateEmptyField(
langTools,
fieldKey !== 'simpleBinding' ? fieldKey : undefined,
);
errors.push(langAsString('form_filler.error_required', [fieldName]));
const errorMessage = node.item.textResourceBindings?.requiredValidation
? langAsString(node.item.textResourceBindings?.requiredValidation)
: langAsString('form_filler.error_required', [fieldName]);

errors.push(errorMessage);

componentValidations[fieldKey] = { errors, warnings };
}
Expand Down