2

In struts-config.xml

<form-bean name="myForm" type="com.validator.CustomeDynaValidatorForm">
      <form-property name="testId" type="java.lang.Long"/>
</form-bean>

In validation.xml

<form name="myForm">
      <field property="testId" depends="required">
        <msg name="required" key="test ID required."/>
      </field>

</form>

In validation-rules.xml

<validator name="required"
            classname="com.validator.CustomFieldChecks"
               method="validateRequired"
               methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
                  msg=""/>

In CustomFieldChecks.java

public static boolean validateRequired(Object bean, ValidatorAction va, Field field, ActionMessages errors, 
                                           Validator validator, HttpServletRequest request)
    {
        String value = null;

        if (isString(bean))
        {
            value = (String) bean;
        }
        else
        {
            value = ValidatorUtils.getValueAsString(bean, 
                                                    field.getProperty());
            **// here value is 0 , when the field is left blank in UI** 
        }

        if (GenericValidator.isBlankOrNull(value))
        {
            //add error message

            return false;
        }
        else
        {
            return true;
        }
    }

Can someone plz let me know , how do i make sure that if the field is left blank in UI , the value should be null not 0 . Is there a way to do so??? I am using struts 1.2

JAB
  • 3,546
  • 9
  • 36
  • 39
  • Are you getting any error? – NPKR Dec 27 '12 at 12:47
  • @Pradeep : no i am not getting any error . Actually i want to throw an error message if field is left empty.But since its coming as 0 when left blank so its returning true and hence passing the validation – JAB Dec 27 '12 at 12:49

4 Answers4

3

AFAIK, with Struts1, you should use String and not Long to represent a value entered by the user, because that's the only way for Struts to populate your form bean with what the user actually entered and let you redisplay the input page with erroneous values.

Struts will try to transform an empty string to a Long, and will initialize the Long to 0 if the string doesn't represent a valid long value. That's one of the many weak points of Struts.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

Yes . Found the fix . In web.xml used below code

<init-param>
      <param-name>convertNull</param-name>
      <param-value>true</param-value>
</init-param>

So this converts the default 0 to null.

JAB
  • 3,546
  • 9
  • 36
  • 39
0

It's because your field exist, it just return null when doesnt exists

Dav
  • 330
  • 2
  • 10
0
GenericValidator.isBlankOrNull(value) behavior is 

Checks if the field isn't null and length of the field is greater than zero not including whitespace.

Hence your converting all incoming values to String in your code. '0' also a not empty so it not going in if condition.

You can change your code like here

if (GenericValidator.isBlankOrNull(value) ) {
    ...
} else if (value != null && value.equals("0")) {
//Add here your sepecial error message for zero

} 
NPKR
  • 5,368
  • 4
  • 31
  • 48
  • I agree this would work , but i need to display some other error message when user explicitly enter 0. – JAB Dec 27 '12 at 12:59
  • In the both the cases the control would come to else if section only .. Because if user leaves it blank , if condition fails as you said , if user explicitly enters 0 again if condition fails , hence in both the cases the displayed error message would be the same... – JAB Dec 27 '12 at 13:10