Skip to content

Commit

Permalink
fix(forms): Fix bug on replacing error message values (#2971)
Browse files Browse the repository at this point in the history
  • Loading branch information
henit authored Dec 8, 2023
1 parent c270a79 commit b24ddb5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import userEvent from '@testing-library/user-event'
import { Form, DataContext, Field } from '../../../'
import { Props as StringFieldProps } from '../../../Field/String/String'
import { JSONSchema7 } from 'json-schema'
import nbNO from '../../../../../shared/locales/nb-NO'

const nb = nbNO['nb-NO'].Forms

function TestField(props: StringFieldProps) {
return <Field.String {...props} validateInitially continuousValidation />
Expand Down Expand Up @@ -582,6 +585,30 @@ describe('DataContext.Provider', () => {
})
})

it('should show default errorMessages based on outer schema validation with injected value', () => {
const schema: JSONSchema7 = {
type: 'object',
properties: {
val: {
type: 'string',
minLength: 486,
},
},
}

render(
<DataContext.Provider schema={schema} data={{ val: 'abc' }}>
<TestField path="/val" />
</DataContext.Provider>
)

expect(
screen.getByText(
nb.stringInputErrorMinLength.replace('{minLength}', '486')
)
).toBeInTheDocument()
})

it('should call "onSubmitRequest" on invalid submit set by a schema', () => {
const onSubmitRequest = jest.fn()

Expand Down
35 changes: 6 additions & 29 deletions packages/dnb-eufemia/src/extensions/forms/Field/Number/Number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,37 +61,14 @@ function NumberComponent(props: Props) {
const errorMessages = useMemo(
() => ({
required: tr.inputErrorRequired,
minimum: tr.numberFieldErrorMinimum.replace(
'{minimum}',
props.minimum?.toString()
),
maximum: tr.numberFieldErrorMaximum.replace(
'{maximum}',
props.maximum?.toString()
),
exclusiveMinimum: tr.numberFieldErrorExclusiveMinimum.replace(
'{exclusiveMinimum}',
props.exclusiveMinimum?.toString()
),
exclusiveMaximum: tr.numberFieldErrorExclusiveMaximum.replace(
'{exclusiveMaximum}',
props.exclusiveMaximum?.toString()
),
multipleOf: tr.numberFieldErrorMultipleOf.replace(
'{multipleOf}',
props.multipleOf?.toString()
),
minimum: tr.numberFieldErrorMinimum,
maximum: tr.numberFieldErrorMaximum,
exclusiveMinimum: tr.numberFieldErrorExclusiveMinimum,
exclusiveMaximum: tr.numberFieldErrorExclusiveMaximum,
multipleOf: tr.numberFieldErrorMultipleOf,
...props.errorMessages,
}),
[
tr,
props.errorMessages,
props.minimum,
props.maximum,
props.exclusiveMinimum,
props.exclusiveMaximum,
props.multipleOf,
]
[tr, props.errorMessages]
)
const schema = useMemo<JSONSchema7>(
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,12 @@ function StringComponent(props: Props) {
const errorMessages = useMemo(
() => ({
required: tr.inputErrorRequired,
minLength: tr.stringInputErrorMinLength.replace(
'{minLength}',
props.minLength?.toString()
),
maxLength: tr.stringInputErrorMaxLength.replace(
'{maxLength}',
props.maxLength?.toString()
),
minLength: tr.stringInputErrorMinLength,
maxLength: tr.stringInputErrorMaxLength,
pattern: tr.inputErrorPattern,
...props.errorMessages,
}),
[tr, props.errorMessages, props.minLength, props.maxLength]
[tr, props.errorMessages]
)
const schema = useMemo<JSONSchema7>(
() =>
Expand Down

0 comments on commit b24ddb5

Please sign in to comment.