-
-
Notifications
You must be signed in to change notification settings - Fork 52.2k
why we always need callback()
in validator function at Form component? 为什么表单验证的 rules 中使用 validator 时,其方法体总是需要调用 callback()
#5155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It's by designed: https://github.com/yiminghe/async-validator/ BTW, how does async-validator know an asynchronous procedure is end, if you don't use |
被人吐槽了 https://www.zhihu.com/question/33629737/answer/150154145 可以加一个文档说明,我们做开源的原则之一是兵来将挡水来土掩。 |
文档已更新。 |
你好是否 这种用法 只能用 class 的写法? |
这个API设计真的是很不优雅,感受下我只是想做一下某个checkbox是否选中的检查: rules: [
{
message: 'You need to agree to our terms to sign up.',
validator: (rule, value, cb) => (value === true ? cb() : cb(true)),
},
], 并且给到callback的第一个参数是错误信息,和 大部分使用者预期的使用方式应该是直接返回 rules: [
{
message: 'You need to agree to our terms to sign up.',
validator: (rule, value, cb) => value === true,
},
], 可用,但是还是建议优化这个API,即使用的是 https://github.com/yiminghe/async-validator/,但是ANT这边完成全可以在调用上封装一下 |
@neekey 可以去给 async-validator 提个兼容的 PR |
@neekey 这种奇怪的接口是为了实现:
有没有更好的方式啊? |
<Row>
<Col span={24} key={"method"}>
<FormItem {...formItemMethodLayout} label="请求方法">
{getFieldDecorator("method", {
initialValue: this.state.data.method,
validateTrigger: "onChange",
rules: [{
required: true,
message: `请选择请求方法`
}, {
//validator: this.state.checkUnique ? this.checkConfirmMethod.bind(this) : (rule, value, callback) => {callback();},
validator: (这如何判断当前组件是否开始校验了) ?
this.checkConfirmMethod.bind(this) : (如果不是当前组件开始校验,就调用其他函数),
}]
})(
<Select placeholder={"请选择请求方法"} onChange={::this.handleChange.bind(this)}>
{this.state.apiRequestMethodSelect}
</Select>
)}
</FormItem>
</Col>
</Row |
每次碰到函数的校验,都会来这里看一次,真的不好记 |
validator 函数
不是统一callback errors么,也挺合理的啊感觉 |
在请求后,后台返回的回调里面怎么调用这个消息提示 |
在使用rule中validator 做自定义校验,遇到一个问题 |
你 |
有人提了,yiminghe说,反对。callback 更直接。都9102年了,还用callback,坐等真香 |
我把自定义校验的方案改了,在onFieldsChange()中判断,不符合自定义的规则就直接改errors,然后就会在控件中显示error信息 |
校验通过后下面的提示不消失是怎么回事 |
问一个问题,这个validator能不能在页面加载的时候,不做校验? |
@neekey <Form>
<Form.Item ...>
{getFieldDecorator('name', {validator: (rule, value, callback) => setTimeout(callback(), 1000)})(
<Input placeholder="name" />
)}
</Form.Item>
.... and before the setTimeout is fired, the whole component is unmounted. Then the timeout is fired, its callback is fired and component will usesetState (I guess) which will result in the following error:
Sadly the callback is not cancellable ! No way to avoid this error! |
@jiufengdadi the |
为什么我自定义验证通过了 还出现async-validator: [""] , |
如果有两个校验的话,第一个会失效,为什么?
|
rules: [
{
required: true,
message: '请输入手机号',
},
{
validator: (rule, value, callback) => {
try {
if (this.props.form.getFieldValue('prefix') == 86 && value) {
const reg = /^1\d{10}$/;
if (!reg.test(value)) {
throw new Error('Something wrong!');
}
}
} catch (err) {
callback(err);
return // +
}
callback() // +
},
message: '请输入有效手机号',
},
], |
你解决了吗,遇到同个情况 |
message提示不会重复吗?'请输入手机号 请输入有效手机号' 类似这样 |
不会 |
rule的validator 成功失败与否都要调用callback,区别是传不传参数 |
我也遇到了这个问题, 如果initialvalue为空, 在正常输入内容后通过onblur时间可以正常触发自定义校验器并有错误的情况下会进行提示, 但一旦初始initialvalue是有值的情况下, 在validatefields 进行所有表单组件统一校验时, err打印出来是null, 过了一会后 又会在控制台弹出校验失败, 自定义验证器是异步action |
我觉得你把函数里的return;删掉,再试一试,或许有用。
…------------------ 原始邮件 ------------------
发件人: "Ivan-hl"<notifications@github.com>;
发送时间: 2019年11月20日(星期三) 下午5:19
收件人: "ant-design/ant-design"<ant-design@noreply.github.com>;
抄送: "wk"<1109243952@qq.com>; "Manual"<manual@noreply.github.com>;
主题: Re: [ant-design/ant-design] why we always need `callback()` in validator function at Form component? 为什么表单验证的 rules 中使用 validator 时,其方法体总是需要调用 callback() (#5155)
在使用rule中validator 做自定义校验,遇到一个问题
validateFunc = (rule, value, callback) => {
if (true) {
console.log(11);
callback('Error message');
return;
}
callback();
};
我想要校验结果'Error message'在对应的FormItem组件下显示,但是callback执行后,'Error message'没有在对应的FormItem组件下显示,而是在控制台打印出来,这是为什么?
在使用rule中validator 做自定义校验,遇到一个问题
validateFunc = (rule, value, callback) => {
if (true) {
console.log(11);
callback('Error message');
return;
}
callback();
};
我想要校验结果'Error message'在对应的FormItem组件下显示,但是callback执行后,'Error message'没有在对应的FormItem组件下显示,而是在控制台打印出来,这是为什么?
你解决了吗,遇到同个情况
我也遇到了这个问题, 如果initialvalue为空, 在正常输入内容后通过onblur时间可以正常触发自定义校验器并有错误的情况下会进行提示, 但一旦初始initialvalue是有值的情况下, 在validatefields 进行所有表单组件统一校验时, err打印出来是null, 过了一会后 又会在控制台弹出校验失败, 自定义验证器是异步action
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
太坑了 |
校验函数里面如果出现异常,没有任何提示。 |
我在想这个AntdV是不维护了么 |
同样遇到这个问题,没下文了? |
|
请问你这个问题解决了吗?同样碰到了这个问题 |
|
validatorFun(rule, value, callback) { |
我遇到的问题是我使用了a-radio-group的,然后我只对其进行空值校验,因为有需求要在选择两个条件后发起请求,所以又对radio-group做了@change监听,两个单选组必须都选择了才发起请求,此时用@change来validate这两个条件值是否为空,一开始为空时,正常提示,后面两个值都不为空时不走validate().then()而是走的catch(error)为什么?而且console.log(error)打印出来的errorFields是空的,value显示两个校验字段都有值了,百思不得解。
|
|
2022 年了,还callback嘛? |
我在提交前进行所有字段校验时报错,定位到是因为一个自定义校验,但不知道为什么会报错。
提交后的校验
|
2023年了。。请问这个 validator: ()=> boolean的方法有计划支持么。。 |
解决了么 |
感谢提供 Form 等一系列组件。
问题描述
由于要在提交前,需要对表单内的各个 field 进行再次校验,使用了
validateFieldsAndScroll
。但发现并未生效。后来经调试,发现如果对该函数传入一组指定的 field,是可以生效的。但只要这组 fileds 中,包含了任意一个在校验规则中指定使用
validator
辅助校验的,则整个validateFields
方法都不生效。最后,才发现,是因为,我一开始并没有在对应的
validator
回调中,总是返回callback()
(觉得很多余,而且一开始不写也可以对单个 input 做正常校验)。加上以后就 ok 了。疑惑
请问,这属于 bug 吗? 因为文档中并没有指出,
validator
中必须要返回callback()
。如果确实是必须要返回
callback
,建议要在文档中提醒开发者注意,或者默认可以缺省。代码片段
render()
Environment
The text was updated successfully, but these errors were encountered: