【问题标题】:How to use Yup validation schema with the Controller component in `react-hook-form` lib如何在“react-hook-form”库中将 Yup 验证模式与 Controller 组件一起使用
【发布时间】:2020-11-05 22:06:51
【问题描述】:
  • 我们有一个想要使用的第 3 方组件SecondaryInput
  • 想使用Yup 验证(不知道如何在Controller 组件中将yup 与规则道具一起使用)
  • 受控组件在子组件内,所以我使用useFormContext
  • 架构代码不起作用

我的代码是这样的

注意:我不能在自定义组件中使用 ref,因为它不需要像 ref 这样的 props

父组件

  const schema = yup.object().shape({
    patientIdentity: yup.object().shape({
      firstName: yup.string().required('Required field'),
      lastName: yup.string().required('Required field'),
    }),
  });
  const methods = useForm();
  const {
    errors,
    handleSubmit,
    register,
    setValue,
    reset,
    getValues,
  } = useForm({
    defaultValues: {
      patientIdentity: {
        firstName: 'test 1',
        lastName: 'Test 2',
      },
    },
    validationSchema: schema,
  });

  const onSubmit = (data, e) => {
    console.log('onSubmit');
    console.log(data, e);
  };

  const onError = (errors, e) => {
    console.log('onError');
    console.log(errors, e);
  };
  console.log('errors', errors); // Not able to see any any error 
  console.log('getValues:> ', getValues()); Not able to see any any values
  return (
    <View style={[t.flex1]}>
      {/* Removed code from here */}
      <View style={[t.flex1, t.selfCenter]}>
       
        <FormProvider {...methods}>
          <View style={[t.flexCol]}>
            <PatientForm /> // <<Child component
            <PrimaryButton
              style={[t.pL3, t.h10]}
              onPress={handleSubmit(onSubmit, onError)}
            >
              Save changes
            </PrimaryButton>
          </View>
        </FormProvider>
      </Row>
    </View>

子组件

  const {
    control,
    register,
    getValues,
    setValue,
    errors,
    defaultValuesRef,
  } = useFormContext();

  console.log('errors:>>', errors); // NOt able to log
  console.log('Identity | getValues>', getValues());
  return (
    <DetailCard title="Identity">
      <Controller
        control={control}
        render={(props) => {
          const { onChange, onBlur, value } = props;
          return (
            <SecondaryInput
              label="Legal First Name"
              value={value}
              onTextChange={(value) => onChange(value)}
              onBlur={onBlur}
            />
          );
        }}
        name="patientIdentity.firstName"
        rule={register({
          required: ' name cannot be empty',
          })}
        defaultValue=""
      />
      <Controller
        control={control}
        as={({ onChange, onBlur, value }) => {
          return (
            <SecondaryInput
              label="Legal Last Name"
              value={value}
              onTextChange={(value) => onChange(value)}
              onBlur={onBlur}
            />
          );
        }}
        name="patientIdentity.lastName"
        rules={{
          required: 'this is required field',
          message: 'required field',
        }}
        defaultValue=""
      />
</DetailCard>
)

【问题讨论】:

    标签: reactjs forms react-native yup react-hook-form


    【解决方案1】:

    您需要检查几件事:

    • schema 对象不依赖于任何本地范围的变量。你可以把它放在函数之外,这样组件就不会在每次渲染时都重新创建它

    • 首先将 yup schema 对象传递给 yupResolver,而不是直接传递给 resolver

    import React from 'react';
    import { useForm } from 'react-hook-form';
    import { yupResolver } from '@hookform/resolvers/yup';
    import * as yup from "yup";
    
    const schema = yup.object().shape({
      patientIdentity: yup.object().shape({
        firstName: yup.string().required('Required field'),
        lastName: yup.string().required('Required field'),
      }),
    });
    
    const App = () => {
      const { ... } = useForm({
        resolver: yupResolver(schema),
      });
    
      return (...);
    };
    
    • 如果您使用第三方库(如 Yup)编写验证规则,则可以从 Controller 中删除 rules 属性,因为它们是重复的。

    【讨论】:

    猜你喜欢
    • 2020-08-22
    • 2021-10-05
    • 2021-11-05
    • 1970-01-01
    • 2021-06-19
    • 2020-11-23
    • 1970-01-01
    • 2022-11-22
    • 2022-10-13
    相关资源
    最近更新 更多