React-redux-form: How to submit form in react native

Created on 4 Sep 2016  路  3Comments  路  Source: davidkpiano/react-redux-form

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.

question

Most helpful comment

It works:) thank you for your help.

All 3 comments

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!

Was this page helpful?
0 / 5 - 0 ratings