Simple example of using the requiredif Validator rule in Struts

By: Apache Foundation Emailed: 1589 times Printed: 2041 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

First off, there's an even newer Validator rule called validwhen, which is almost certainly what you want to use, since it is much easier and more powerful. It will be available in the first release after 1.1 ships. The example shown below could be coded with validwhen as:

<form name="medicalStatusForm">

<field
    property="pregnancyTest" depends="validwhen">
  <arg0 key="medicalStatusForm.pregnancyTest.label"/>
  <var>
    <var-name>test</var-name>
    <var-value>((((sex == 'm') OR (sex == 'M')) AND (*this* == null)) OR (*this* != null))</test>
  </var>
</field>

Let's assume you have a medical information form with three fields, sex, pregnancyTest, and testResult. If sex is 'f' or 'F', pregnancyTest is required. If pregnancyTest is not blank, testResult is required. The entry in your validation.xml file would look like this:

<form name="medicalStatusForm">

<field
    property="pregnancyTest" depends="requiredif">
  <arg0 key="medicalStatusForm.pregnancyTest.label"/>
  <var>
    <var-name>field[0]</var-name>
    <var-value>sex</var-value>
  </var>
  <var>
    <var-name>fieldTest[0]</var-name>
    <var-value>EQUAL</var-value>
  </var>
  <var>
    <var-name>fieldValue[0]</var-name>
    <var-value>F</var-value>
  </var>
  <var>
    <var-name>field[1]</var-name>
    <var-value>sex</var-value>
  </var>
  <var>
    <var-name>fieldTest[1]</var-name>
    <var-value>EQUAL</var-value>
  </var>
  <var>
    <var-name>fieldValue[1]</var-name>
    <var-value>f</var-value>
  </var>
  <var>
    <var-name>fieldJoin</var-name>
    <var-value>OR</var-value>
  </var>
</field>

<field
    property="testResult" depends="requiredif">
  <arg0 key="medicalStatusForm.testResult.label"/>
  <var>
    <var-name>field[0]</var-name>
    <var-value>pregnancyTest</var-value>
  </var>
  <var>
    <var-name>fieldTest[0]</var-name>
    <var-value>NOTNULL</var-value>
  </var>
</field>
</form>

Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Thanks for the tutorial,

I have a query:
For a customer Registration, if customer account type is Current account, then amount field must be mandatory, and if customer type is savings/salary account then amount is not mandatory, how can I perform this validation


View Tutorial          By: rajendra at 2013-04-29 15:37:28

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Apache Foundation
When is the best time to validate input in Struts
Chaining actions in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling Duplicate Form Submissions in Struts
What is Struts? Which Version of Struts to use?
Struts 1 vs Struts 2
ForwardAction in Struts
ActionErrors and ActionError in Struts
Using JavaScript to submit a form in Struts
Model 1 Architecture
Introduction to Struts Architecture
DispatchAction in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
MVC Architecture (Model 2 Architecture)
Configuring JDBC DataSources in Struts
Struts and Tiles - Steps to use Struts and Tiles
Most Emailed Articles (in Struts)
Downloading and installing Struts
The directories and files of a Struts application
Editing web.xml in a Struts Application
Editing struts-config.xml in a Struts Application
Creating the first application using Struts 2
Model 1 Architecture
MVC Architecture (Model 2 Architecture)
Installing and configuring Tomcat and Struts
Guidelines for Struts Application Development
MVC with configurable controller
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
ForwardAction in Struts
Using JavaScript to submit a form in Struts
Simple example of using the requiredif Validator rule in Struts