如何使用 Immutable.js 处理 Redux-Form FieldArrays 的验证?
How to handle validation for Redux-Form FieldArrays with Immutable.js?
我目前正在使用 redux-form/immutable、Wizard-Form 和 FieldArrays 为项目创建多步骤表单。我基本上陷入了如何使用 immutable.js 验证字段数组的问题。
我按照以下示例进行操作:https://redux-form.com/8.1.0/examples/immutable/ to setup validation in validate.js file with immutable but I also had to follow the following example https://redux-form.com/8.1.0/examples/fieldarrays/ 了解如何对 FieldArrays 进行验证。
按照这两个例子,我得出了以下结论:
if (values.get('customTrips')) {
const customTripArrayErrors = [];
values.get('customTrips').forEach((member, memberIndex) => {
const customTripMemberErrors = {};
if (!member.get('from_location_address1') {
}
}
}
当我尝试上面的代码时,出现以下错误:
Uncaught TypeError: Cannot read property 'get' of undefined
我只需要帮助弄清楚如何访问 validate.js 中的 FieldArray 成员值。
如有任何帮助,我们将不胜感激!谢谢!
我的想法是正确的,但是在推送到 Redux-Form FieldArray 时我错过了一个重要的步骤。您需要确保在推送时创建了一个新的 Map,以便您可以使用 get 访问这些值。如果您不创建新地图并且您 console.log(member),您将看到它是未定义的。希望这会有所帮助,因为 Redux-Form 文档中没有写!
import { Map } from 'immutable';
<FlatButton
type="button"
onClick={() => fields.push(new Map())}
backgroundColor={Color.primary}
hoverColor={Color.primaryDarkened}
className="left"
label="Add a Local Trip"
/>
我目前正在使用 redux-form/immutable、Wizard-Form 和 FieldArrays 为项目创建多步骤表单。我基本上陷入了如何使用 immutable.js 验证字段数组的问题。
我按照以下示例进行操作:https://redux-form.com/8.1.0/examples/immutable/ to setup validation in validate.js file with immutable but I also had to follow the following example https://redux-form.com/8.1.0/examples/fieldarrays/ 了解如何对 FieldArrays 进行验证。
按照这两个例子,我得出了以下结论:
if (values.get('customTrips')) {
const customTripArrayErrors = [];
values.get('customTrips').forEach((member, memberIndex) => {
const customTripMemberErrors = {};
if (!member.get('from_location_address1') {
}
}
}
当我尝试上面的代码时,出现以下错误:
Uncaught TypeError: Cannot read property 'get' of undefined
我只需要帮助弄清楚如何访问 validate.js 中的 FieldArray 成员值。
如有任何帮助,我们将不胜感激!谢谢!
我的想法是正确的,但是在推送到 Redux-Form FieldArray 时我错过了一个重要的步骤。您需要确保在推送时创建了一个新的 Map,以便您可以使用 get 访问这些值。如果您不创建新地图并且您 console.log(member),您将看到它是未定义的。希望这会有所帮助,因为 Redux-Form 文档中没有写!
import { Map } from 'immutable';
<FlatButton
type="button"
onClick={() => fields.push(new Map())}
backgroundColor={Color.primary}
hoverColor={Color.primaryDarkened}
className="left"
label="Add a Local Trip"
/>