开发者

validation of multiple fields in jsf 1.x

i need to va开发者_开发技巧lidate multiple fields in the form, individual field validations are working fine. i wanted to know if all the fields are empty is there any way with jsf 1.x and richfaces 3.3 to display a single message like "all fields are mandatory" instead of each validation message


you can have one list that contains all the error messages, and the list gets populated onsubmit.

example:

    public String doBid() {
     boolean flag=false;
    errorMessages = new ArrayList<String>();
    if (getUserID().equals("")) {
   flag=true;
    }
    if (getKeyword().equals("")) {
     flag=true;
    }
    if (getNumericBidAmount() == 0.00) {
    flag=true;
    }
    if (getNumericBidDuration() =0) {
     flag=true;

    }
     if (flag==true)
         errorMessages.add("all fields are mandatory");
    if (errorMessages.size() > 0) {
    return(null);
    } else {
    return("success");
    }
    14 }


    ---------

    public String getErrorMessages() {
    String messageList;
    if ((errorMessages == null) ||
    (errorMessages.size() == 0)) {
    messageList = "";
    } else {
    messageList = "<FONT COLOR=RED><B><UL>\n";
    for(String message: errorMessages) {
    messageList = messageList + "<LI>" + message + "\n";
    }
    messageList = messageList + "</UL></B></FONT>\n";
    }
    return(messageList);
    }

    --------------------
    <h:form>
    <h:outputText value="#{bidBean1.errorMessages}"
    escape="false"/>
    <TABLE>
    <TR>
    <TD>User ID:
    <h:inputText value="#{bidBean1.userID}"/></TD></TR>
    <TR>
    <TD>Keyword:
    <h:inputText value="#{bidBean1.keyword}"/></TD></TR>
    <TR>
    <TD>Bid Amount:
    $<h:inputText value="#{bidBean1.bidAmount}"/></TD></TR>
    <TR>
    <TD>Duration:
    <h:inputText value="#{bidBean1.bidDuration}"/></TD></TR>
    <TR><TH>
    <h:commandButton value="Send Bid!"
    action="#{bidBean1.doBid}"/></TH></TR>
    </TABLE>
    </h:form>


I'm using MyFaces Extensions Validator + a simple plugin with a ProcessedInformationRecorder for doing exactly what you are talking about.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜