Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Formik based form samples #1

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open

Formik based form samples #1

wants to merge 29 commits into from

Conversation

reinhard
Copy link
Member

No description provided.

@reinhard reinhard requested a review from mepheser April 20, 2018 07:18
Copy link
Member

@mepheser mepheser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error handling

// see https://github.com/jaredpalmer/formik/issues/288
const {user, setErrors} = action
if (user.name.startsWith('G')) {
setErrors({name: 'Names starting with \'G\' are not allowed.'})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reinhard Why not check this directly in Form using name: yup.string().matches(insertRegexHere).required()?
If this is an example for server side errors: I think these should be treated the "normal" redux way, meaning an FORMS_POST_USER_ERROR action is thrown and reducer saves userPostError.

This userPostRestError may be injected to UserForm and used as "external" error to be added to internal formik errors like

const UserForm = ({user, onSubmit, restError}:Props) => {
  return (
    <Formik
      key={user.id + user.lastModified.toString()}
      displayName="UserForm"
      onSubmit={(values, {setErrors}) => onSubmit(values, setErrors)}
      initialValues={user}
      validateOnChange={false}
      validationSchema={validationSchema}
      render={({values, errors, touched, setErrors}) => {
       if (restError) {
           // setErrors(..add external restErrors if present)
       }
        return (
          <Form>
            <FormRow name="name" label="Name" errors={errors} touched={touched} />
            <FormRow name="email" label="E-Mail" errors={errors} touched={touched} />
            <AddressesForm values={values} errors={errors} touched={touched} />
            <Box mt={2}>
              <ButtonLink>
                <Link to="/forms">Cancel</Link>
              </ButtonLink>
              <button type="submit">Save</button>
              //  or render "general" rest errors like "internal server error" without binding to a field
              {(restError) && <div> Error saving user: {restError}
            </Box>
          </Form>
        )
      }}
    />
  )
}

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

Successfully merging this pull request may close these issues.

None yet

2 participants