Reactjs 验证redux-form的textfield中的prop
我为一项任务挣扎了几个小时。 我有一个来自material ui的redux表单文本字段,我使用它的方式如下:Reactjs 验证redux-form的textfield中的prop,reactjs,redux-form,react-intl,Reactjs,Redux Form,React Intl,我为一项任务挣扎了几个小时。 我有一个来自material ui的redux表单文本字段,我使用它的方式如下: <Field id="searchCif" name="searchCif" component={TextField} floatingLabelText={SEARCHVIEW_HINT_CIF} disabled={(afm !== undefined)}
<Field
id="searchCif"
name="searchCif"
component={TextField}
floatingLabelText={SEARCHVIEW_HINT_CIF}
disabled={(afm !== undefined)}
validate={[requireValidator, onlyNumeric]}
/>
我使用intl是因为我的信息应该被翻译。但是错误显示,intl.formatted message不是函数。因此我写道:
validate={()=>[requireValidator(value,intl),onlyNumeric(value,int)]}
。错误未显示,但验证工作不正常。有什么想法吗?您的验证函数工作不正常,因为Validate prop是一个带有value和allValues参数的函数。将函数包装到另一个函数中,以传入附加参数
const requireValidator = intl => value => (
(value === undefined) ?
intl.formatMessage({ id: 'error.search.cif.afm' }) : undefined
);
const requireValidatorInternationalized = requireValidator(intl);
const onlyNumeric = intl => value => (
(value !== undefined && !(/^([0-9])+$/g).test(value)) ?
intl.formatMessage({ id: 'error.search.cif.afm.only.number' }) :
undefined
);
const onlyNumericInternationalized = onlyNumeric(intl);
<Field
id="searchCif"
name="searchCif"
component={TextField}
floatingLabelText={SEARCHVIEW_HINT_CIF}
disabled={(afm !== undefined)}
validate={[requireValidatorInternationalized, onlyNumericInternationalized]}
/>
const requireValidator=intl=>value=>(
(值===未定义)?
intl.formatMessage({id:'error.search.cif.afm'}):未定义
);
const requireValidator internationalized=requireValidator(intl);
常量onlyNumeric=intl=>value=>(
(值!==未定义的&&!(/^([0-9])+$/g)。测试(值))?
intl.formatMessage({id:'error.search.cif.afm.only.number'}):
未定义
);
const onlyNumericInternationalized=onlyNumeric(intl);
Erikras(redux表单存储库的所有者和主要贡献者)定义参数化验证器的单个实例,而不是从Validate属性传递参数,以防止不必要的字段重新呈现(例如,不要执行Validate={[requiredValidator(intl),onlyNumeric(intl)]
).您作为intl传递的是什么?在您的代码中,console.log(typeof`which you's passing in as intl')会输出什么?intl是区域设置和string类型您是否导入intl以获得formatMessage方法?我能看看你的import/require语句吗;根据文档,您似乎应该执行formatMessage(intl,{id:''})此语法正确吗?const requireValidator=intl=>value=>value==未定义?intl.formatMessage({id:'error.search.cif.afm'}):未定义
const requireValidator = intl => value => (
(value === undefined) ?
intl.formatMessage({ id: 'error.search.cif.afm' }) : undefined
);
const requireValidatorInternationalized = requireValidator(intl);
const onlyNumeric = intl => value => (
(value !== undefined && !(/^([0-9])+$/g).test(value)) ?
intl.formatMessage({ id: 'error.search.cif.afm.only.number' }) :
undefined
);
const onlyNumericInternationalized = onlyNumeric(intl);
<Field
id="searchCif"
name="searchCif"
component={TextField}
floatingLabelText={SEARCHVIEW_HINT_CIF}
disabled={(afm !== undefined)}
validate={[requireValidatorInternationalized, onlyNumericInternationalized]}
/>