Skip to content

Added support for functions as validation message props #12

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

Merged
merged 2 commits into from
Aug 15, 2019
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,35 +156,35 @@ The step size between the `min` and `max` values. If invalid, the `stepMismatch`

The message to display [when the input is invalid somehow](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-bad-input).

#### `patternMismatch?: string`
#### `patternMismatch?: string|(value?: any, props: Object) => string`

The message to display when the value does not match the pattern specified by the `pattern` prop.

#### `rangeOverflow?: string`
#### `rangeOverflow?: string|(value?: any, props: Object) => string`

The message to display when the value is higher than the `max` prop.

#### `rangeUnderflow?: string`
#### `rangeUnderflow?: string|(value?: any, props: Object) => string`

The message to display when the value is lower than the `min` prop.

#### `stepMismatch?: string`
#### `stepMismatch?: string|(value?: any, props: Object) => string`

The message to display the value is not one of the valid steps specified by the `step` prop.

#### `tooLong?: string`
#### `tooLong?: string|(value?: any, props: Object) => string`

The message to display when the value longer than the value specified by the `maxLength` prop.

#### `tooShort?: string`
#### `tooShort?: string|(value?: any, props: Object) => string`

The message to display when the value shorter than the value specified by the `minLength` prop.

#### `typeMismatch?: string`
#### `typeMismatch?: string|(value?: any, props: Object) => string`

The message to display when the value does not match the `type` prop.

#### `valueMissing?: string`
#### `valueMissing?: string|(value?: any, props: Object) => string`

The message to display when the value is required, but missing.

Expand Down
5 changes: 4 additions & 1 deletion src/Html5ValidationField.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export default class Html5ValidationField extends React.Component<Props> {
return validity.customError
}
const errorKey: ?string = errorKeys.find(key => (validity: Object)[key])
const error = errorKey && this.props[errorKey]
let error = errorKey && this.props[errorKey]
if (typeof error === 'function') {
error = error(value, this.props)
}
input.setCustomValidity(error)
return error
}
Expand Down
29 changes: 29 additions & 0 deletions src/Html5ValidationField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,5 +537,34 @@ describe('Html5ValidationField', () => {
}
)
})
it('should support functions as default error keys', () => {
const setCustomValidity = jest.fn()
mockFindNode(
{
nodeName: 'input',
setCustomValidity,
validity: {
tooShort: true
}
},
() => {
const spy = jest.fn(({ input }) => <input {...input} />)
TestUtils.renderIntoDocument(
<Form initialValues={{ foo: 'bar' }} onSubmit={onSubmitMock} subscription={{}}>
{() =>
<Html5ValidationField
tooShort={(value, { minLength }) =>
`Value ${value} should have at least ${minLength} characters.`}
minLength={8} name="foo" render={spy} />
}
</Form>
)
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(2)
expect(spy.mock.calls[1][0].meta.error).toBe('Value bar should have at least 8 characters.')
}
)
})
})

})
20 changes: 11 additions & 9 deletions src/types.js.flow
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// @flow
import type { FieldProps } from 'react-final-form'

type MessageValue = string | ((value?: any, props: Object) => string)

type Messages = {
badInput?: string,
patternMismatch?: string,
rangeOverflow?: string,
rangeUnderflow?: string,
stepMismatch?: string,
tooLong?: string,
tooShort?: string,
typeMismatch?: string,
valueMissing?: string
badInput?: MessageValue,
patternMismatch?: MessageValue,
rangeOverflow?: MessageValue,
rangeUnderflow?: MessageValue,
stepMismatch?: MessageValue,
tooLong?: MessageValue,
tooShort?: MessageValue,
typeMismatch?: MessageValue,
valueMissing?: MessageValue
}
export type Html5ValidationFieldProps = FieldProps & Messages