Hi,
I have form in my React Native app:
<Form model="user" onSubmit={user => onSubmit(user)}>
<Field model="user.name">
<TextInput onBlur={() => validate('user.name', () => false)}/>
</Field>
<TouchableHighlight onPress={onPress} underlayColor={'gray'} style={styles.button}>
<Text>{text}</Text>
</TouchableHighlight>
</Form>
but I don't know how to submit form in proper way (in onPress function).
I know that I can use action.submit but then I will bypass for example submit prevention when fields are invalid (for test user.name field is forced to invalid).
You are do checks on handleSubmit in form component and I would like to use them but I don't know how to submit form programatically.
Instead of using <Form>, have a handler on your parent component, such as:
// ...
handleSubmit() {
const { userForm, user } = this.props;
if (userForm.valid) { // userForm.$form.valid in V1
// submit user here
} else {
// show errors
}
}
and have the onPress={...} handler of <TouchableHighlight> call handleSubmit.
It works:) thank you for your help.
@davidkpiano
How to use onSubmit without a button, how to use something like onChange on the form ?
Thanks!
Most helpful comment
It works:) thank you for your help.