redux 表单中基于 json 的验证问题
Posted
技术标签:
【中文标题】redux 表单中基于 json 的验证问题【英文标题】:json based validation issue in redux form 【发布时间】:2018-06-13 04:39:08 【问题描述】:在下面的 redux 表单中基于 json 执行验证的问题是
**json creating dynamic form**
const simplefield = [
name: "firstname",
type: "text",
placeholder: "Enter Name",
isrequired: "true",
maxLength: "5",
minLength: "3",
checkrules:
requiredfield: message: "required firstname" ,
maxlength: value: 5, message: "maxlength" ,
minlength: value: 3, message: "minlength"
,
name: "age", type: "number", placeholder: "Enter age" ,
name: "email",
type: "email",
placeholder: "Enter Email",
checkrules:
requiredfield: message: "required" ,
,
defaultValue: "email@email.com"
,
]
验证码
const validate = (values, props) =>
const validation = props.fields.filter(field => field.checkrules);
const error = ;
validation.map((v, i) =>
if ("requiredfield" in v.checkrules)
if (values[v.name] === undefined || values[v.name] === "")
return (error[v.name] = v.checkrules.requiredfield.message);
else if (!/\S+@\S+\.\S+/.test(values.email))
return (error.email = "invalid");
);
在这里,我正在创建一个基于 json 的 redux 表单,该表单呈现字段(我能够实现它),但我坚持基于下面 json 中名为 checkrules 的字段执行动态验证,如下是实现验证的代码没有正确实现,请给我解决方案
【问题讨论】:
【参考方案1】:export const createModalValidation = value =>
const errors = ;
if (!value.rules || !value.rules.length)
errors.rules = _error: 'At least one member must be entered' ;
else
const rulesArrayErrors = [];
value.rules.forEach((member, memberIndex) =>
const ruleErrors = ;
if (!member || !(member.name == 'your_key'))
here can check any number of condition...
ruleErrors.fact = 'Required';
rulesArrayErrors[memberIndex] = ruleErrors;
);
if (rulesArrayErrors.length)
errors.rules = rulesArrayErrors;
return errors;
【讨论】:
感谢您的回答,您能告诉我应该在值参数中传递什么 value 是我提交表单check here后得到的值 是的,但是我怎么知道这个特定的字段正在按要求进行验证,最小长度等等,我向你展示了带有 checkrules 对象的 json,并且里面有验证规则 是的,你可以通过 value.key > maxLength 给出,反之亦然,通过错误消息给出 minLength。以上是关于redux 表单中基于 json 的验证问题的主要内容,如果未能解决你的问题,请参考以下文章