gpt4 book ai didi

javascript - Reactjs - Material UI - 减少表单框架 - 分组复选框需要验证错误修复以至少需要一个复选框

转载 作者:行者123 更新时间:2023-12-03 13:40:23 25 4
gpt4 key购买 nike

enter image description here
我正在做一个 react 项目,并构建了一个表单框架,它围绕 redux 表单包装了 Material ui。
** 沙盒 https://codesandbox.io/s/condescending-banzai-re6l3
我不确定如何修复错误 - 如果分组复选框字段设置为验证 - 仅在复选框组下出现必需的消息 - 然后要求所有复选框都是必需的。
这是当前的代码库

//renderGroupedCheckboxes

import React from "react";
import { Field } from "redux-form";

import FormControl from "@material-ui/core/FormControl";
import FormLabel from "@material-ui/core/FormLabel";
import FormGroup from "@material-ui/core/FormGroup";
//import FormHelperText from '@material-ui/core/FormHelperText';

import renderCheckboxField from "./renderCheckboxField";

const renderGroupedCheckboxes = (fields) => (
<FormControl component="fieldset" fullWidth={true}>
<FormLabel component="legend">{fields.label}</FormLabel>
<FormGroup row>
{fields.names.map((item, j) => {
return (
<Field
key={j}
name={item}
component={renderCheckboxField}
label={fields.options[j].label}
onChange={function (e, newVal) {
fields.onHandle(e.target.name, newVal);
}}
/>
);
})}
</FormGroup>
</FormControl>
);

export default renderGroupedCheckboxes;
单个复选框
    //renderCheckboxField

import React from "react";
import Checkbox from "@material-ui/core/Checkbox";
import FormControl from "@material-ui/core/FormControl";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import FormHelperText from "@material-ui/core/FormHelperText";

const renderCheckboxField = ({
input,
rows,
multiline,
label,
meta: { touched, error, warning }
}) => (
<FormControl component="fieldset">
<FormControlLabel
label={label}
control={<Checkbox />}
checked={input.value ? true : false}
{...input}
/>
<FormHelperText error={error && error.length > 0 ? true : false}>
{(error && error.length > 0 ? error : null) ||
(warning && warning.length > 0 ? warning : null)}
</FormHelperText>
</FormControl>
);

export default renderCheckboxField;

最佳答案

修改 renderGroupedCheckboxes.js。我们正在检查是否有任何复选框被触摸并且所有复选框都有错误,然后呈现错误文本

import React from "react";
import { Field } from "redux-form";

import FormControl from "@material-ui/core/FormControl";
import FormLabel from "@material-ui/core/FormLabel";
import FormGroup from "@material-ui/core/FormGroup";
import FormHelperText from "@material-ui/core/FormHelperText";

import renderGroupedCheckboxField from "./renderGroupedCheckboxField";

const renderGroupedCheckboxes = (fields) => {
let error = null;
let isAnyFieldTouched = false;
let isAllFieldsError = true;

for (let name of fields.names) {
//if touched is true for any one field, isAnyFieldTouched will
//be true
isAnyFieldTouched = isAnyFieldTouched || fields[name].meta.touched;

if (
error === null &&
fields[name].meta.error &&
fields[name].meta.error.length > 0
) {
error = fields[name].meta.error;
}

//if invalid is false for any one field, isAllFieldsError will
//be false
isAllFieldsError = isAllFieldsError && fields[name].meta.invalid;
}

return (
<FormControl component="fieldset" fullWidth={true}>
<FormLabel component="legend">{fields.label}</FormLabel>
<FormGroup row>
{fields.names.map((item, j) => {
return (
<Field
key={j}
name={item}
component={renderGroupedCheckboxField}
label={fields.options[j].label}
onChange={function (e, newVal) {
fields.onHandle(e.target.name, newVal);
}}
/>
);
})}
</FormGroup>
<FormHelperText error={error && error.length > 0 ? true : false}>
{isAnyFieldTouched && isAllFieldsError && error && error.length > 0
? error
: null}
</FormHelperText>
</FormControl>
);
};

export default renderGroupedCheckboxes;
如下创建 renderGroupedCheckboxField.js。我们创建新文件的原因是它不会影响复选框字段的当前行为。
import React from "react";
import Checkbox from "@material-ui/core/Checkbox";
import FormControl from "@material-ui/core/FormControl";
import FormControlLabel from "@material-ui/core/FormControlLabel";

const renderGroupedCheckboxField = ({
input,
rows,
multiline,
label,
meta: { touched, error, warning }
}) => (
<FormControl component="fieldset">
<FormControlLabel
label={label}
control={<Checkbox />}
checked={input.value ? true : false}
{...input}
/>
</FormControl>
);

export default renderGroupedCheckboxField;
沙盒 - https://codesandbox.io/s/funny-fast-f3i12?file=/src/_SharedFormComponents/renderGroupedCheckboxes.js

关于javascript - Reactjs - Material UI - 减少表单框架 - 分组复选框需要验证错误修复以至少需要一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66338294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com