Material-UI 自动完成:如何设置为必填字段

Sus*_*.96 6 reactjs material-ui

我正在使用 Material UI 自动完成(多个)构建一个表单。我需要该字段为必填项。但是当我提交带有选定值的表单时,它总是显示该字段是必填字段的消息。仅当选项 multiple = true 时才会出现此问题。

有人知道如何解决这个问题吗?

沙盒中的代码示例

Chr*_*ris 8

尝试检查文本字段的输入属性中输入值数组的长度是否为 0。

<Autocomplete
      includeInputInList={false}
      options={options}
      getOptionLabel={(option) => option.name}
      onChange={(event, newValue) => {
        setValue(newValue);
      }}
      value={value}
      renderInput={(params) => (
        <TextField
          {...params}
          variant="standard"
          label={"Names"}
          inputProps={{
            ...params.inputProps,
            autoComplete: "new-password",
            required: value.length === 0
          }}
          required={true}
        />
      )}
Run Code Online (Sandbox Code Playgroud)

codeandbox中的示例