所以我按照 Dojo - Using the Dojo JavaScript Library to Build Ajax Applications 中的示例将服务器端验证添加到表单上的用户名验证文本框字段。基本上我添加了一个提交 xhrGet 请求的 usernameOnChange 函数,xhrGet 返回 JSON 并由 usernameValidationHandler 处理。
它工作得很好,但 usernameValidationHandler 仅将工具提示显示消息设置为错误。它不会将该字段设置为无效,因此用户仍然可以提交表单。如何将字段设置为无效,以便表单不会提交?
<input type="text" id="userName" name="userName" size="20"
dojoType="dijit.form.ValidationTextBox"
trim="true"
required="true"
onChange="userNameOnChange"
regExp="\w+"
invalidMessage="User name is required"
/>
function userNameOnChange() {
var userName = dijit.byId("userName").getValue();
if (userName == "") {
return;
}
dojo.xhrGet( {
url: "validateUserName.jsp?userName=" + userName,
handleAs: "json",
handle: userNameValidationHandler
});
}
function userNameValidationHandler(response) {
dijit.byId("userName").displayMessage();
if (!response.valid) {
var errorMessage = "User name already taken";
// Display error message as tooltip next to field
dijit.byId("userName").displayMessage(errorMessage);
// HOW DO I SET THE FIELD TO BE INVALID NOW???
}
}