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

Added support for functions as validation message props #12

Merged
merged 2 commits into from
Aug 15, 2019

Conversation

BowlingX
Copy link
Contributor

@BowlingX BowlingX commented Mar 26, 2019

Thank you so much for this library.
This pull request will add the ability to specify functions as message props:

Example

<Html5ValidationField
  tooShort={(value, { minLength }) => `Value ${value} should have at least ${minLength} characters.`}
  minLength={8} 
  name="foo"
/>

This is especially usefull when defining default translation messages for all fields (here with react-intl):

import { injectIntl, defineMessages } from 'react-intl'

const messages = defineMessages({
   valueMissing: 'Value missing',
   minLength: 'Value should have at least {minLength} characters',
   /*... more messages ...*/
})
const defaultMessages = {
  valueMissing: (_, { formatMessage } ) => formatMessage(messages.valueMissing),
  minLength: (_, { formatMessage, minLength } ) => formatMessage(messages.minLength, { minLength }),
/*... more messages ...*/
}
const FieldWithDefaults = injectIntl(({ intl: { formatMessage }, ...rest }) => <Field formatMessage={formatMessage} {...defaultMessages} {...rest } />)

Currently this requires to create an object on each render with default messages based on the Field's props. I think this would provide a more elegant and performant way.

@codecov
Copy link

codecov bot commented Mar 26, 2019

Codecov Report

Merging #12 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #12   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines          36     38    +2     
  Branches       16     17    +1     
=====================================
+ Hits           36     38    +2
Impacted Files Coverage Δ
src/Html5ValidationField.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 090a232...dab9c8a. Read the comment docs.

@BowlingX
Copy link
Contributor Author

Anything else you require for merging / checking this PR?

@BowlingX
Copy link
Contributor Author

BowlingX commented Aug 6, 2019

@erikras This pull request is already quite long in the pipeline, do you request any more comment / feedback, tests etc.?

@erikras erikras merged commit 7b1638a into final-form:master Aug 15, 2019
@erikras
Copy link
Member

erikras commented Jun 12, 2020

Published in v1.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants