Javascript 多个验证函数被视为或,而不是和
我试图遵循官方网站上的字段级验证示例 这是我用来渲染字段的Javascript 多个验证函数被视为或,而不是和,javascript,reactjs,redux-form,Javascript,Reactjs,Redux Form,我试图遵循官方网站上的字段级验证示例 这是我用来渲染字段的renderInput函数 const renderInput = ({ input, label, type, meta }) => ( <React.Fragment> <input {...input} type={type} /> <pre>{JSON.stringify({meta}, null, 4)}</pre
renderInput
函数
const renderInput = ({
input,
label,
type,
meta
}) => (
<React.Fragment>
<input
{...input}
type={type}
/>
<pre>{JSON.stringify({meta}, null, 4)}</pre>
</React.Fragment>
)
现在发生的是
。。。因为其中1个为真,验证通过
如果其中一个元素为false,validate
数组不应该为false吗
或者我看到的是错误的吗?从他们的示例中,似乎
验证
数组中传入的每个函数都是按顺序运行的,第一个没有返回未定义
(如果有)的函数将为该字段定义元.error
。从他们的示例中,validate
数组中传入的每个函数似乎都是按顺序运行的,第一个不返回undefined
(如果有)的函数将为该字段定义meta.error
。当您执行文档中的操作时会发生什么-putconst minLength10=minLength(10)
并将其放入数组中 当您执行文档中的操作时会发生什么-将const minLength10=minLength(10)
放入数组中 你的意思是让导出const minLength=min=>value=>吗?是的,我从另一个文件导入它们,然后在这里导入。但这与被问的问题无关,不是无关的。为什么minLength
返回另一个函数?听起来像是在问,从概念上讲,当一个条件失败时通过时,单词validate
是否适合在这里使用。正确吗?当您执行文档中的操作时会发生什么-将const minLength10=minLength(10)
放入数组中。不过我只是在抓救命稻草。它应该是&&
您的意思是让导出const minLength=min=>value=>
吗?是的,我从不同的文件导入它们,并在这里导入。但这与被问的问题无关,不是无关的。为什么minLength
返回另一个函数?听起来像是在问,从概念上讲,当一个条件失败时通过时,单词validate
是否适合在这里使用。正确吗?当您执行文档中的操作时会发生什么-将const minLength10=minLength(10)
放入数组中。不过我只是在抓救命稻草。它应该是&&
我相信是因为传递的新函数每次都会导致元素的重新呈现,所以它实际上是无效的,但没有显示在UI中,因为它被重置了。但是如果没有剩下的代码,我就不确定了。很高兴这对你有用,但我相信这是因为传递的新函数每次都会导致元素的重新呈现,所以它实际上是无效的,但没有显示在UI中,因为它被重置了。但是如果没有剩下的代码,我就不确定了。很高兴这对你有用
<Field
name="title"
component={renderInput}
validate={[required, minLength(10)]}
type="text"
/>
const required = value => {
console.log('required', !!(value && value.length > 0) ? undefined : 'Mandatory')
return !!(value && value.length > 0) ? undefined : 'Mandatory';
};
const minLength = min => value => {
console.log(`minLength(${min})`, !!(value && value.length < min) ? `More than ${min} characters please` : undefined);
return !!(value && value.length < min) ? `More than ${min} characters please` : undefined;
}
if (required('test') && minLength(10)) // false
if (required('test') || minLength(10)) // true