Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React antd form disable submit button

Tags:

reactjs

antd

How could I disable form submit button using antd

Form validations work, but I need to visually disable form submit button when validation fails

enter image description here

This is a stackblitz url https://stackblitz.com/edit/react-ts-z5d6tr?file=Hello.tsx

like image 908
Lashan Fernando Avatar asked Nov 25 '20 18:11

Lashan Fernando


People also ask

How to disable a button in react?

We can disable the button by passing a boolean value to the disabled attribute. In the above example, we are using the react hooks useState hook to initialize the state.

What is disabled Disabled and loading in antd?

disabled: when actions are not available. loading: add loading spinner in button, avoiding multiple submits too. There are primary button, default button, dashed button, text button and link button in antd.

How to disable submit button until all required fields are completed?

If you wrap your submit button in a Form.Item like this, it should disable it until all required fields are completed. The rest of the form would remain the same. Show activity on this post.

What are buttons in ant design?

A button means an operation (or a series of operations). Clicking a button will trigger corresponding business logic. In Ant Design we provide 5 types of button. Primary button: indicate the main action, one primary button at most in one section. Default button: indicate a series of actions without priority.


3 Answers

I found some elegant way to perform this

<Form.Item shouldUpdate className="submit">
  {() => (
    <Button
      type="primary"
      htmlType="submit"
      disabled={
        !form.isFieldsTouched(true) ||
        form.getFieldsError().filter(({ errors }) => errors.length)
          .length > 0
      }
    >
      Log in
    </Button>
  )}
</Form.Item>
like image 135
Lashan Fernando Avatar answered Nov 15 '22 04:11

Lashan Fernando


I'm going to paste here my solution in case anyone finds it useful. Note; this solution uses the hook Form.useForm() and the callback onFieldsChange.

The validation is not included in this excerpt. But the idea is that when the form changes (any field) it will set the submit button (in this case inside the Modal) to disabled/enabled.

const LoginForm = () => {
  const [form] = useForm();
  const [disabledSave, setDisabledSave] = useState(true);
  
  const handleFormChange = () => {
    const hasErrors = form.getFieldsError().some(({ errors }) => errors.length);
    setDisabledSave(hasErrors);
  }
  
  return (
    <Modal okButtonProps={{ disabled: disabledSave }} onOk={form.submit}>
      <Form onFieldsChange={handleFormChange} form={form}>
        <Item name="username" label="username">
          <Input />
        </Item>
        <Item name="password" label="password">
          <Input />
        </Item>
      </Form>
    </Modal>
  )
};
like image 36
alextrastero Avatar answered Nov 15 '22 05:11

alextrastero


I found a nice solution here that doesn't involve any additional state. If you wrap your submit button in a Form.Item like this, it should disable it until all required fields are completed. The rest of the form would remain the same.

<Form.Item
 noStyle
 shouldUpdate
>
  {({ getFieldsValue }) => {
    const { username, password } = getFieldsValue();
    const formIsComplete = !!username && !!password;
    return (
      <Button
         type="primary"
         htmlType="submit"
         disabled={!formIsComplete}
       >
         Log In
       </Button>
    );
  }}
</Form.Item>
like image 27
CB721 Avatar answered Nov 15 '22 05:11

CB721