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

FAQ:自定义远程验证(remote)返回的结果格式 #81

Closed
niceue opened this Issue Nov 1, 2013 · 6 comments

Comments

Projects
None yet
4 participants
@niceue
Copy link
Owner

niceue commented Nov 1, 2013

$('#form').validator({
    dataFilter: function(data) {
        // 返回 niceValidator 支持的格式
        return data;
    },
    fields: {
        'username': '用户名: required; username; remote[check/username.php]'
    }
});

或者在字段中传递dataFilter,优先级更高

$('#form').validator({
    fields: {
        'username': {
            rule: '用户名: required; username; remote[check/username.php]',
            dataFilter: function(data) {
                // 返回 niceValidator 支持的格式
                return data;
            }
        }
    }
});

假设后端返回的数据为:

{"type":"error", "msg": "用户名已存在!"}

如果后端不能更改返回数据格式,那么你需要在前端转换一下:

$('#form').validator({
    dataFilter: function(data) {
        var d = {};
        data = $.parseJSON(data);
        d[ data.type ] = data.msg;
        return d;
    },
    fields: {
        'username': '用户名: required; username; remote[check/username.php]'
    }
});
@vanabel

This comment has been minimized.

Copy link

vanabel commented Mar 1, 2014

需要添加一个php的return数据类型的写法.

@hainuo

This comment has been minimized.

Copy link

hainuo commented May 16, 2014

remote[check/username.php]
这里 增加其他参数的话可以这样用吗?

 remote[check/username.php, username, type=add]

我测试的时候没有成功,不知道原因
还有这样一定不成功
remote[check/username.php?type=add]

@niceue

This comment has been minimized.

Copy link
Owner Author

niceue commented May 16, 2014

@hainuo
静态参数都可以通过url传参,暂不支持你上面描述的方式
remote[check/username.php?type=add]这样子没错,这里remote的url传参是同时支持get和post的

@hongweizhiyuan

This comment has been minimized.

Copy link

hongweizhiyuan commented Aug 27, 2014

能不能写一个 username.php 的例子,我不管怎么写,都返回一个 叉号和error。谢谢!

@niceue

This comment has been minimized.

Copy link
Owner Author

niceue commented Aug 27, 2014

@54programmer 官网示例对返回数据格式已经有明确说明

@hongweizhiyuan

This comment has been minimized.

Copy link

hongweizhiyuan commented Aug 27, 2014

@niceue 我也清楚,不过我认为我的也没错,可就是不成功,所以没办法才给你留言的。

@niceue niceue added the question label Jan 17, 2016

@niceue niceue closed this Jan 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment