reactjs - redux-forms pre validate before submission -
i validate form before submission , display common error in bottom, instead of attaching error particular field. errros.fieldname="age > 10". upon no errors, prompt message continue submission , upon confirmation call form-submit.
what correct approach using redux-forms?
beforevalidate(values) { if (<<some validation condition>>) { this.setstate({err:"error"}) return; } else { this.setstate({dialogopen:true, err:""}) } }
to display common error message instead of attaching error.text somewhere in render function
{ this.state.err !='' && <span classname="text text-danger"><strong>no value selected! </strong></span> }
the problem values not defined in beforevalidate function. synch validate provided in redux-form not provide way set common error message. not sure asynch-validate correct place. caught. please help.
you can use normal validate
function , add _error
. error whole form not field.
const validate = ({ options }) => { const errors = {}; if (options.length === 0) errors._error = 'required'; return errors; };
in form render
use this.props.error
check form error. this:
render() { const { handlesubmit, onsubmit, submitting, error, submitfailed } = this.props; return ( <view> {error && <formvalidationmessage> {i18n.t(error)} </formvalidationmessage>} </view> ); }
Comments
Post a Comment