Domanda

i am trying to understand if it is possible to display certain HTML elements on a page based on the errors resulted during the validation of the current page.

For example I have a form which has a text box hidden. When user submits the form and the form has any error, when the user sees the page with error information they should see the hidden text box visible.

Tried to use pseudo code to explain what i am trying below.

Setting error from Spring validation as below:

In the validator class i have something like,

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "mainForm", "mainform.error.display.alternateText",
            "error occured");

In the JSP i have something like,

        <td id="alternateTextBox" 
        <c:choose>
            <c:when test="${not empty mainForm.error}">
                style="width:50%;visibility:visible" >
            </c:when>
            <c:otherwise>
                style="width:50%;visibility:hidden" >
            </c:otherwise>
        </c:choose>
            <s:message code="standing_instructions.RELATIONSHIP" />
            <form:input path="alternateText" id="alternateText" size="50"/>
        </td>
È stato utile?

Soluzione

Yes possible,

try like:

in your validator add a field error:

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "mainForm", "mainform.error.display.alternateText", "mainForm required!");

and in jsp you can show inside form the error like:

<c:set var="er"><form:errors path="mainForm"/></c:set>

<c:if test="${not empty er}">
    <div class="alert alert-error">
     <button type="button" class="close" data-dismiss="alert">&times;</button>                 <h4>Validation Error :</h4>
        Please correct the following:<br/>
        ${er}
    </div>
</c:if>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top