Skip to content

Required Fields for @rjsf/antd #3743

Open
@georgiossalon

Description

@georgiossalon

Prerequisites

What theme are you using?

antd

Version

5.8.2

Current Behavior

Input fields that are set as required by the scheme are missing the required property in the browser.

In the following code, if you switch to the MaterialUI or the default Form component you do get the expected result. When using the antd Form component via the withTheme you don't.

Here is a dummy Form:

import React, {createRef} from "react";
import "./App.less";
import {withTheme} from "@rjsf/core";
import Form from '@rjsf/material-ui';
import validator from "@rjsf/validator-ajv8";

import {Theme as AntDTheme} from '@rjsf/antd';
import {Popconfirm} from "antd";

const RJSFForm = withTheme(AntDTheme);

const uiSchema = {
    "ui:disabled": false,
    "ui:order": [
        "Name1",
        "Number 1",
    ],
    Name1: {
        "ui:disabled": false
    },
    "Number 1": {
        "ui:disabled": false
    },
};

const schema = {
    type: "object",
    $schema: "http://json-schema.org/draft-07/schema#",
    required: ["Name1"],
    "ui:order": [
        "Name1",
        "Number 1",
    ],
    properties: {
        "Name1": {
            type: "string"
        },
        "Number 1": {
            type: "number",
            default: 0
        },
    },
    $id: "http://json-schema.org/draft-07/schema#"
};

function App() {
    const formRef = createRef();
    const submitRef = createRef();
    const onError = (errors) => console.log(errors);

    return (
        <div className="App">
            <RJSFForm
                schema={schema}
                uiSchema={uiSchema}
                validator={validator}
                onError={onError}
                ref={formRef}
                formData={{
                    "Number 1": 0,
                }}
            >
                <button hidden={true} ref={submitRef}/>
            </RJSFForm>
            <button
                onClick={() => {
                    submitRef.current.click();
                }}
            >
                submit
            </button>

        </div>
    );
}


export default App

Expected Behavior

In the browser, the input tags should get the required property when the set into the schema.

Steps To Reproduce

Sandbox: CodeSandbox

Environment

- OS: -
- Node: v16
- npm: v8

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions