async-validator icon indicating copy to clipboard operation
async-validator copied to clipboard

建议在值为空字符串,但不是 required 时,不进行任何校验,同时清除验证结果

Open Riant opened this issue 6 years ago • 3 comments

如题,也即是,除 required 之外的所有校验运行前都必须检查一下该项 rule 是否包含必填,以及值是否为空。

清除验证结果,是因为值为空时,本来就不存在是否验证通过的概念。

这样的话,可以解决不少问题,比如 #127

几年前从 jquery-validator 的源码中学到的,深以为该逻辑非常必要。

Riant avatar Aug 12 '19 06:08 Riant

是这样,目前好像只能在每个规则中进行自定义,来判断是否要校验了

topul avatar Oct 10 '19 09:10 topul

+1

我也遇到这样的问题,比如要验证一个可为空的数值类型,没有required规则,只有一个type: 'number',值为空字符串时验证不通过

dingzeng avatar Jan 13 '20 06:01 dingzeng

+1

我也遇到这样的问题,比如要验证一个可为空的数值类型,没有required规则,只有一个type: 'number',值为空字符串时验证不通过

+1 , 也遇到一样的问题 required: false, type: "string" 为空时能通过校验, 且 null 或 undefined 能通过 required: false, type: "number" 为空字符串时不能校验通过, 但是 null 或 undefined 能通过

Yes, I have the same problem required: false, type: "string" Is empty and can pass the verification and is null or undefined can pass the verification too required: false, type: "number" Is empty string ('') and cannot be verified, but is null or undefined can pass the verification

所以校验 number , 为 '' 时可以将转成 null 或 undefined So, check number. When it is empty string , it can be converted to null or undefined

我的解决办法, 将数据转成 string, 进行处理, 这样处理起来简单一点 My solution is to convert the data into 'string' for processing, it do easy

zhedream avatar Dec 09 '20 02:12 zhedream