一直在尋找這個答案,我想出了我自己的解決方案。我不想在DB - Redis中添加一個字段 - 所以我需要創建一個密碼檢查字段,並驗證它是否與第一個密碼字段匹配。密碼檢查字段將被提交到後端,這不是一個錯誤,而是一個功能。
關於代碼。
我們定義的第一個功能是一個將創建密碼校驗字段,並追加到原來的密碼字段:
function createPassCheck(el) {
// Create the containing table row
var passCheck = $("<tr></tr>").addClass("FormData")
.attr({
id: "tr_passwordCheck",
rowpos: 20
});
// Create a cell for the label and add it to the row
var passCheckLabelTd = $("<td></td>")
.addClass("CaptionTD").text("Password Check");
passCheck.append(passCheckLabelTd);
// Prepare the cell for the input box. All
// the cells have a non breaking space, we add
// one as well to keep aligned. We then add it to the row.
var passCheckTd = $("<td> </td>")
.addClass("DataTD");
passCheck.append(passCheckTd);
// Create an input box, and add it to the input cell
var passCheckInput = $("<input></input>")
.addClass("FormElement ui-widget-content ui-corner-all")
.attr({
id: "passwordCheck",
name: "passwordCheck",
role: "textbox",
type: "password"
});
passCheckTd.append(passCheckInput);
// Finally append the row to the table, we have been called after
// the creation of the password row, it will be appended after it.
var tbodyEl = el.parentNode.parentNode.parentNode;
tbodyEl.appendChild(passCheck[0]);
}
之前,我們可以繼續前進,我們需要添加其他功能,一鍵一:它會檢查兩個密碼是否匹配。
function customPassCheck(cellvalue, cellname) {
// Get a reference to the password check input box. You see
// the 'tr_passwordCheck' id we are using? We set that id in
// the function "createPassCheck".
var passCheckVal = $("#tr_passwordCheck input").val()
// If both fields are empty or the passwords match
// we can submit this form.
if (
(cellvalue == "" && passCheckVal == "")
||
cellvalue == passCheckVal
) {
return [true, ""];
}
// Can you guess why we are here?
return [false, "Password and password check don't match."];
}
最後,我們將用來執行對編輯密碼爲空的功能,我們將通過註冊爲自定義格式做到這一點。
function customPassFormat(cellvalue, options, rowObject) {
// When asked to format a password for display, simply
// show a blank. It will make it a bit easier when
// we editing an object without changing the password.
return "";
}
我們可以在現在的jqGrid定義了密碼字段,並使其特殊:
jQuery("#crud").jqGrid({
....
....
colModel:[
....
{
name:'password',
index:'password',
width:80,
align:"right",
editable:true,
// It is hidden from the table view...
hidden: true,
editrules:{
// ...but we can edit it from the panel
edithidden: true,
// We are using a custom verification
custom:true,
// This is the function we have created
// to verify the passwords
custom_func: customPassCheck
},
edittype: 'password',
// Our custom formatter that will blank the
// password when editing it
formatter: customPassFormat,
editoptions: {
// This is where the magic happens: it will add
// the password check input on the fly when editing
// from the editing panel.
dataInit: createPassCheck
}
},
....
....
這是所有鄉親!
法比奧