formik#yupToFormErrors TypeScript Examples

The following examples show how to use formik#yupToFormErrors. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StaticIpForm.tsx    From assisted-ui-lib with Apache License 2.0 5 votes vote down vote up
StaticIpForm = <StaticIpFormValues extends object>({
  infraEnv,
  updateInfraEnv,
  getInitialValues,
  getUpdateParams,
  validationSchema,
  onFormStateChange,
  getEmptyValues,
  children,
  showEmptyValues,
}: PropsWithChildren<StaticIpFormProps<StaticIpFormValues>>) => {
  const { clearAlerts, addAlert } = useAlerts();
  const { captureException } = useErrorMonitor();
  const [initialValues, setInitialValues] = React.useState<StaticIpFormValues | undefined>();
  React.useEffect(() => {
    if (showEmptyValues) {
      //after view changed the formik should be rendered with empty values
      setInitialValues(getEmptyValues());
    } else {
      setInitialValues(getInitialValues(infraEnv));
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  const handleSubmit: FormikConfig<StaticIpFormValues>['onSubmit'] = async (values) => {
    clearAlerts();
    try {
      const staticNetworkConfig = getUpdateParams(infraEnv, values);
      await updateInfraEnv({
        staticNetworkConfig: staticNetworkConfig,
      });
    } catch (error) {
      captureException(error);
      addAlert({ title: getErrorMessage(error) });
    }
  };
  if (!initialValues) {
    return null;
  }
  const validate = (values: StaticIpFormValues) => {
    try {
      validationSchema.validateSync(values, { abortEarly: false, context: { values: values } });
      return {};
    } catch (error) {
      return yupToFormErrors(error);
    }
  };
  return (
    <Formik
      initialValues={initialValues}
      onSubmit={handleSubmit}
      validate={validate}
      validateOnMount
      enableReinitialize
    >
      <Form>
        {children}
        <AutosaveWithParentUpdate<StaticIpFormValues>
          onFormStateChange={onFormStateChange}
          getEmptyValues={getEmptyValues}
        />
      </Form>
    </Formik>
  );
}
Example #2
Source File: validateOrder.test.ts    From mayoor with MIT License 5 votes vote down vote up
getFormikErrors = async (formValues: any) => {
	try {
		await validateYupSchema(formValues, getOrderValidationSchema(mockedTFunction));
	} catch (err) {
		return yupToFormErrors<OrderFormValues>(err);
	}
}