I have scenario to Ignore the Struts Validation on application back for some of the pages. I have a flow of pages. User completes one page and click "Next" button to enter into another page. While clicking the Next there is Validation check for mandatory elements, and they are working fine.
In the same page there is "Back" button, clicking on that button user is navigated to previous page. User should allow to go to previous page even if mandatory elements not entered. But in my case Mandatory checks are stopping.
I am using Struts Validation Frame work.
I have AdminWorkFlow-Validation.xml
file which has all the required validation for the pages.
How to make Validation check Ignore when clicked on the Back button.
Using Struts 2.0.11 and
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
I have found the solution to my problem. I created my own Validators. Which Handles two things.
Managing multiple series of forms with same action by providing the Wizard Index(Static) and Page Index(Set by Page). Sample Code :
/**
*/
public class MandatoryStringFieldValidator extends FieldValidatorSupport {
// Holds the Current Page Index, value picked from JSP(Front End)
private String pageIndex = "0";
// Holds the Wizard Page which is set in the xxx-validation.xml
private String wizardPage = "0";
// Holds a Value by which I can identify if the action is back button click
//If true validation is ignored. This is also taken from the JSP(Front End)
private String backClicked;
public String getBackClicked() {
return backClicked;
}
public void setBackClicked(String backClicked) {
this.backClicked = backClicked;
}
public String getPageIndex() {
return pageIndex;
}
public void setPageIndex(String pageIndex) {
this.pageIndex = pageIndex;
}
public String getWizardPage() {
return wizardPage;
}
public void setWizardPage(String wizardPage) {
this.wizardPage = wizardPage;
}
@Override
public void validate(Object object) throws ValidationException {
final String fieldName = getFieldName();
final Object value = getFieldValue(fieldName, object);
if(!"0".equals(pageIndex)){
Integer pageIndexValue = (Integer) getFieldValue(pageIndex, object);
if (pageIndexValue != null) {
backClicked = (String) getFieldValue(backClicked, object);
if(StringUtils.isNotBlank(backClicked) && Boolean.valueOf(backClicked)){
pageIndexValue = pageIndexValue - 1;
}
pageIndex = String.valueOf(pageIndexValue);
}
}
if(wizardPage.equals(pageIndex)) {
if (!(value instanceof String)) {
addFieldError(fieldName, object);
return;
}
String strValue = (String) value;
if (StringUtils.isBlank(strValue)) {
addFieldError(fieldName, object);
}
}
}
}
Validation.xml
<validators>
<validator name="requiredStringValidator"
class="com.client.validation.MandatoryStringFieldValidator"/>
</validators>
Finally in -validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="serviceMot.driverName">
<field-validator type="requiredStringValidator" short-circuit="true">
<param name="pageIndex">pageIndex</param>
<param name="backClicked">backClicked</param>
<param name="wizardPage">1</param>
<message>${getText('page1.mandatoryName')}</message>
</field-validator>
</validators>
Please Note : I have stripped of bit of code as it has production code and Changed the names in the file as well.