Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

validation does not work with useController #799

Open
mmahalwy opened this issue Jan 13, 2022 · 0 comments
Open

validation does not work with useController #799

mmahalwy opened this issue Jan 13, 2022 · 0 comments

Comments

@mmahalwy
Copy link

Bug Report

Describe the Bug

validation does not work with useController

How to Reproduce

const validateRequired = (value) => {
  console.log(value);

  return false;
};

const Field = ({ name, ...restProps }) => {
  const [fieldProps] = useControlled(name, {
    ...restProps,
    defaultValue: null,
    formId: 'form1',
    validate: validateRequired,
  });

  return (
    <Form.Item label="Label">
      <Input {...fieldProps} />
    </Form.Item>
  );
};

function CoolForm() {
  count++;

  const { form, focus, use } = useForm({
    defaultValues: {
      test: [{ firstName: 'Bill', lastName: 'Luo' }],
    },
    onSubmit: (values) => console.log(values),
    onError: (errors) => console.log('onError: ', errors),
    id: 'form1',
  });
  const [fields, { push, insert, remove, swap, move }] = useFieldArray('test', { formId: 'form1' });

  /* console.log(
    "Field value: ",
    use({ value: "foo", touched: "touched.foo", dirty: "dirty.foo" })
  ); */

  return (
    <form ref={form}>
      <div className="count">Render {count} times</div>
      <Field name={`all`} />

...

Submit, and notice it submits correctly although does not pass validation

CodeSandbox Link

Show me the bug on CodeSandbox.

Expected Behavior

Tell me what should happen.

Screenshots

Add screenshots to help explain your problem.

Your Environment

  • Device: [e.g. MacBook Pro, iPhone12]
  • OS: [e.g. macOS, iOS, Windows]
  • Browser: [e.g. Chrome, Safari]
  • Version: [e.g. v1.0.0]

Additional Information

Any other information about the problem here.

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

No branches or pull requests

1 participant