Fork me on GitHub
<< back to Core Developers Guide Edit on GitHub

conversion validator


Field Validator that checks if a conversion error occurred for this field.



<!-- Plain Validator Syntax -->
<validator type="conversion">
        <param name="fieldName">myField</param>
     <message>Conversion Error Occurred</message>
<!-- Field Validator Syntax -->
<field name="myField">
   <field-validator type="conversion">
      <message>Conversion Error Occurred</message>

Repopulating Field upon conversion Error

The capability of auto-repopulating the stack with a fake parameter map when a conversion error has occurred can be done with repopulateField property set to true.

This is typically useful when one wants to repopulate the field with the original value when a conversion error occurred. Eg. with a textfield that only allows an Integer (the action class have an Integer field declared), upon conversion error, the incorrectly entered integer (maybe a text ‘one’) will not appear when dispatched back. With repopulateField property set to true, it will, meaning the textfield will have ‘one’ as its value upon conversion error.

<!-- myJspPage.jsp -->
<s:form action="someAction" method="POST">
  <s:textfield label="My Integer Field" name="myIntegerField" />
  <s:submit />
<!-- xwork.xml -->
<include file="xwork-default.xml" />
<package name="myPackage" extends="xwork-default">
  <action name="someAction" class="">
     <result name="input">myJspPage.jsp</result>
<!-- -->
public class MyActionSupport extends ActionSupport {
   private Integer myIntegerField;
   public Integer getMyIntegerField() { return this.myIntegerField; }
   public void setMyIntegerField(Integer myIntegerField) {
      this.myIntegerField = myIntegerField;
<!-- MyActionSupport-someAction-validation.xml -->
  <field name="myIntegerField">
     <field-validator type="conversion">
        <param name="repopulateField">true</param>
        <message>Conversion Error (Integer Wanted)</message>