使用JQuery验证器(Cakephp)进行服务器端验证

时间:2022-08-24 22:26:04

Hi i am currently doing a school project using cakePHP. I have the following form and assume that the form tag is created.

嗨,我目前正在使用cakePHP做一个学校项目。我有以下形式,并假设已创建表单标记。

<?php echo $form->input('number',array('id'=>"number",'title'=>"Please enter a number with at least 3 and max 15 characters ha!"));?>

<?php echo $form->input('secret',array('id'=>"secret"));?>

<?php echo $form->input('math',array('id'=>"math",'title'=>"Please enter the correct result!"));?>

 <?php echo $form->input('userName',array('id'=>"userName",'title'=>"User Exist"));?>

I am using a client side validation and here is the code that:

我正在使用客户端验证,这里的代码是:

$.validator.addMethod("buga", function(value) {
    return value == "buga";
}, 'Please enter "buga"!');

// this one requires the value to be the same as the first parameter
$.validator.methods.equal = function(value, element, param) {
    return value == param;
};

    $().ready(function() {
    var validator = $("#texttests").bind("invalid-form.validate", function() {
        $("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below.");
    }).validate({
        debug: true,
        errorElement: "em",
        errorContainer: $("#warning, #summary"),
        errorPlacement: function(error, element) {
            error.appendTo( element.parent("td").next("td") );
        },
        success: function(label) {
            label.text("ok!").addClass("success");
        },
        rules: {
            "data[User][number]": {
                required:true,
                minlength:3,
                maxlength:15,
                number:true 
            },
            "data[User][secret]": "buga",
            "data[User][math]": {
                equal: 11   
            }
        },
                    submitHandler: function(form) {
                        form.submit();
                    }


    });

});

The above only does client side validation can do for the first three inputs but my last input for this case may require to check the username is unique. How am i able to do the ajax call to the server? I am just confused in that part. Hope someone can guide me.

以上只有客户端验证可以对前三个输入执行,但我对此案例的最后输入可能需要检查用户名是否唯一。我怎么能够对服务器进行ajax调用?我只是对那部分感到困惑。希望有人能指导我。

1 个解决方案

#1


5  

jQuery validate has a specific rule for server side validation (remote)

jQuery validate有一个特定的服务器端验证规则(远程)

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

The page above contains all of the information you need to make the request, the second example is pretty much your use case.

上面的页面包含了发出请求所需的所有信息,第二个示例几乎就是您的用例。

The remote script should return only text, either true or false.
See this page:

远程脚本应仅返回true或false的文本。看这个页面:

http://jquery.bassistance.de/validate/demo/milk/

http://jquery.bassistance.de/validate/demo/milk/

open up your firebug/chrome console and look at the request after you fill in an email address and click submit.

打开您的firebug / chrome控制台,在填写电子邮件地址并单击“提交”后查看请求。

Good luck with your project.

祝你的项目好运。

#1


5  

jQuery validate has a specific rule for server side validation (remote)

jQuery validate有一个特定的服务器端验证规则(远程)

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

The page above contains all of the information you need to make the request, the second example is pretty much your use case.

上面的页面包含了发出请求所需的所有信息,第二个示例几乎就是您的用例。

The remote script should return only text, either true or false.
See this page:

远程脚本应仅返回true或false的文本。看这个页面:

http://jquery.bassistance.de/validate/demo/milk/

http://jquery.bassistance.de/validate/demo/milk/

open up your firebug/chrome console and look at the request after you fill in an email address and click submit.

打开您的firebug / chrome控制台,在填写电子邮件地址并单击“提交”后查看请求。

Good luck with your project.

祝你的项目好运。