for循环以验证reduxForm中的值

时间:2018-04-06 09:58:01

标签: javascript reactjs redux-form

我想在提交我的值之前进行验证,以便不为我想要创建for循环的所有字段编写相同的代码,以便验证所有这些字段。更具体地说:

const validate = values => {
const errors = {};
if (!values.measuring_point) {
    errors.measuring_point = i18n.t("Required");
}
if (values.image) {
    if (!/^image.*$/.test(values.image.type)) {
        errors.image = i18n.t("Only image files are allowed");
    }
}
let i = null;
let measurements = [values.measurement_1, values.measurement_2, values.measurement_3, values.measurement_4, values.measurement_5, values.measurement_6];
let errors_measurements = [errors.measurement_1, errors.measurement_2, errors.measurement_3, errors.measurement_4, errors.measurement_5, errors.measurement_6];
let types = [values.measurement_type_1, values.measurement_type_2, values.measurement_type_3, values.measurement_type_4, values.measurement_type_5, values.measurement_type_6];
let errors_types = [errors.measurement_type_1, errors.measurement_type_2, errors.measurement_type_3, errors.measurement_type_4, errors.measurement_type_5, errors.measurement_type_6];
let unit = [values.measurement_unit_1, values.measurement_unit_2, values.measurement_unit_3, values.measurement_unit_4, values.measurement_unit_5, values.measurement_unit_6];
let errors_unit = [errors.measurement_unit_1, errors.measurement_unit_2, errors.measurement_unit_3, errors.measurement_unit_4, errors.measurement_unit_5, errors.measurement_unit_6];
for (i = 1; i < 7; i++) {
    if (types[i] && measurements[i] && !unit[i]) {
        errors_unit[i] = i18n.t("Required");
    }
}
return errors;};

但它不起作用,也没有任何错误

1 个答案:

答案 0 :(得分:0)

我修好了Tho Vu你是对的:

const validate = values => {
const errors = {};
if (!values.measuring_point) {
    errors.measuring_point = i18n.t("Required");
}
if (values.image) {
    if (!/^image.*$/.test(values.image.type)) {
        errors.image = i18n.t("Only image files are allowed");
    }
}
let measurements = [
    values.measurement_1,
    values.measurement_2,
    values.measurement_3,
    values.measurement_4,
    values.measurement_5,
    values.measurement_6
];
let errors_measurements = [
    errors.measurement_1,
    errors.measurement_2,
    errors.measurement_3,
    errors.measurement_4,
    errors.measurement_5,
    errors.measurement_6
];
let types = [
    values.measurement_type_1,
    values.measurement_type_2,
    values.measurement_type_3,
    values.measurement_type_4,
    values.measurement_type_5,
    values.measurement_type_6
];
let errors_types = [
    errors.measurement_type_1,
    errors.measurement_type_2,
    errors.measurement_type_3,
    errors.measurement_type_4,
    errors.measurement_type_5,
    errors.measurement_type_6
];
let unit = [
    values.measurement_unit_1,
    values.measurement_unit_2,
    values.measurement_unit_3,
    values.measurement_unit_4,
    values.measurement_unit_5,
    values.measurement_unit_6
];
let errors_unit = [
    "measurement_unit_1",
    "measurement_unit_2",
    "measurement_unit_3",
    "measurement_unit_4",
    "measurement_unit_5",
    "measurement_unit_6"
];
for (let i = 0; i < 6; i++) {
    if (types[i] && measurements[i] && !unit[i]) {
        errors[errors_unit[i]] = i18n.t("Required");
    }
}
return errors;};