A wrapper around the reduxForm HOC exported from
redux-form that gives it some extra functionality:
- Makes extra options available for configuring the form
- Wraps every
onSubmitresult in a promise. Additionally, wraps rejectedonSubmitresults in aSubmissionError. If the thrown error has anerrorsproperty, its value will be passed toSubmissionError. Else, if the thrown error has amessageproperty, this will be passed to aSubmissionErroras a form-wide error. The original error will be accessible via theSubmissionErrorsmeta.errorproperty. This enables developers to access useful information regarding the origin of the failure, e.g., HTTP status. - Provides a default
onSubmitfunction that resolves successfully and logs a warning. - Ignores any
onChangeevents that occur on a pristine and untouched form, patching a bug inredux-form v8.
The extra options that can be provided to lpForm are as follows:
nameString An alias for"form"- a unique identifier for the form.initialValuesFiltersObject An object with analloworrejectkey pointing to an array of attribute names. The indicated attributes will be omitted from the form'sinitialValues.submitFiltersObject Another filter object that will be used to filter the form values that are submitted.constraintsObject Contraints that will be used to validate the form using the validateWithOptions function.validationOptionsObject An object to pass in any options specified byvalidateJS.beforeSubmitFunction A function that will be called with the form values beforeonSubmit. The options/props oflp-formare provided as the second argument.debounceSubmitNumber An integer representing the time in milliseconds to wait before submitting the form.
import { Field } from 'redux-form'
import { lpForm } from 'lp-form'
import { Input, SubmitButton } from 'lp-components'
function MyForm ({ handleSubmit }) {
return (
<form onSubmit={ handleSubmit }>
<Field name="name" component={ Input } />
<SubmitButton> I'll submit the form! </SubmitButton>
</form>
)
}
export default compose(
lpForm({
name: 'my-form',
initialValuesFilters: { reject: ['id'] },
constraints: { name: { presence: true } },
})
)(MyForm)A wrapper around the validate function exported from
Validate JS to make it work seamlessly with
Redux Form.
constraintsObject A 'flat' object containing constraints in the format specified by Validate JS. These are key-value pairs where the keys correspond to keys in the data that will be validated. This is a 'flat' object in that nested data must be accessed using a string path (ex. 'foo.bar') as the key.valuesObject A nested object containing values to be validated.optionsObject An object to pass in any options specified byValidate JS. (optional, default{})
const values = {
name: 'Foo',
address: {
zip: '12'
}
}
const options = {
fullMessages: true,
}
const constraints = {
name: {
presence: true
},
'address.zip': {
presence: true,
length: { is: 5 }
}
}
validateWithOptions(constraints, values, options)
// {
// address: {
// zip: ['Zip is the wrong length (should be 5 characters)']
// }
// }Returns Object errors - A nested object of errors that will be passed to redux form.
A wrapper around the validate function exported from
Validate JS to make it work seamlessly with
Redux Form.
constraintsObject A 'flat' object containing constraints in the format specified by Validate JS. These are key-value pairs where the keys correspond to keys in the data that will be validated. This is a 'flat' object in that nested data must be accessed using a string path (ex. 'foo.bar') as the key.valuesObject A nested object containing values to be validated.
const values = {
name: 'Foo',
address: {
zip: '12'
}
}
const constraints = {
name: {
presence: true
},
'address.zip': {
presence: true,
length: { is: 5 }
}
}
// Function is curried so this call will work
validate(constraints)(values)
// {
// address: {
// zip: ['Zip is the wrong length (should be 5 characters)']
// }
// }Returns Object errors - A nested object of errors that will be passed to redux form.