3
嗨,我目前正在使用cakePHP做一個學校項目。我有以下形式並假定表單標籤已創建。服務器端驗證使用JQuery驗證器(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"));?>
我使用的是客戶端驗證和這裏是代碼:
$.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();
}
});
});
上面不僅客戶端驗證能夠前三投入做的,但我對這種情況下最後的輸入可能要求檢查用戶名是否是唯一的。我怎麼能夠做到服務器的Ajax調用?我只是困惑在那一部分。希望有人能指導我。