amis icon indicating copy to clipboard operation
amis copied to clipboard

FormItem 的验证可以扩展一下 远程验证逻辑吗?

Open icodingx opened this issue 6 years ago • 11 comments

icodingx avatar Jan 17 '20 05:01 icodingx

https://baidu.github.io/amis/form/validation

2betop avatar Jan 17 '20 06:01 2betop

最后一个是服务端验证

2betop avatar Jan 17 '20 06:01 2betop

这个是提交表单的时候服务器返回验证错误,可不可以,onChange 的时候服务器验证呢?

icodingx avatar Apr 07 '20 07:04 icodingx

如果你能传入 function,那么 form 那层有个 onValidate(values:{[propName:string] => any}) => void | string;

2betop avatar Apr 07 '20 07:04 2betop

更正一下,是可以返回 promise 的。

onValidate(values:{[propName:string] => any}) => void |{[propName:string]: string | string[]} | Promise<void | {[propName:string]: string | string[]}>;

2betop avatar Apr 07 '20 07:04 2betop

可以给我示例吗 ?

icodingx avatar Apr 07 '20 07:04 icodingx

{
    type: 'form',
    controls: [
        {
            type: 'text',
            name: 'a',
            label: 'A'
        }
    ],
    onValdiate(values) {
        if (values.a === '233') {
            return {
                a: 'A 不能是 233'
            }
        }
    }
}

2betop avatar Apr 07 '20 09:04 2betop

谢谢

icodingx avatar Apr 07 '20 09:04 icodingx

onValdiate(values) {
 // 在这里可以发异步请求吗?
}

icodingx avatar Apr 07 '20 09:04 icodingx

{
    type: 'form',
    controls: [
        {
            type: 'text',
            name: 'a',
            label: 'A'
        }
    ],
    async onValdiate(values) {
        const response = await axios(xxx);
        if (values.a === '233') {
            return {
                a: 'A 不能是 233'
            }
        }
    }
}

onValidate 可以返回一个 Promise<{[propName: string]: string | string[]}>

2betop avatar Apr 07 '20 10:04 2betop

The last one is server-side validation

404 Page not found error

phxgbl avatar Jan 31 '22 11:01 phxgbl