Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Struts
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Struts section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2007, 07:16 AM
Registered User
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validation using Validation Framework

Hi Experts,

  I am facing a problem with Validation Framework. Could you solve my problem. I want to validate a field in which the entered value should not be a negative number.
For Example: if i enter -1 or -2 it should give a validation message. Please give me solution with an example. I tried with min but no use. Thanks in Advance.

Thanks,
Kalyan.

 
Old January 2nd, 2008, 06:53 AM
Registered User
 
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi kalyan,

for the same thing i have worked on this couple of weeks back. you need to implement u r own validation rule for this negative integer not to accept . for that u need to write code in validation-rues.xml file,validation.xml and java class for supporting condition. here is the code for u r reference.
        </validator>

   <validator name="myNegativeIf"
                      classname="com.oomc.servicing.icar.web.validator.M yFieldChecks"
                         method="myNegativeIf"
                   methodParams="java.lang.Object,
                                 org.apache.commons.validator.ValidatorAction,
                                 org.apache.commons.validator.Field,
                                 org.apache.struts.action.ActionErrors,
                                 javax.servlet.http.HttpServletRequest"
                        depends=""
                            msg="errors.myNegativeIf">

                   <javascript>

                   </javascript>

            </validator>



 <field property="contract.contractDefault.thresholdAmount "
                   depends="myNegativeIf,integer">

                <arg0 key="label.contract.threshholdAmount"/>

            </field>





   // Negative number validation added by vimal 12/18/2007
    public static boolean myNegativeIf(
       Object bean,
       ValidatorAction va,
       Field field,
       ActionErrors errors,
       HttpServletRequest request ) {


       String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        int value1=0;
      try{
        value1= Integer.parseInt(value);
      }
         catch (Exception e) {
         }

      // System.out.println("Value if int not negative is "+value1);
  // System.out.println("value = " + value + "XXXXX");

       //String sProperty2 = field.getVarValue("secondProperty");
       //String value2 = ValidatorUtil.getValueAsString(bean, sProperty2);
  // System.out.println("value2 = " + value2 + "YYYYY");


  if (!GenericValidator.isBlankOrNull(value)) {

          try{
               if (value1 < 0) {

                  errors.add(field.getKey(),Resources.getActionError (request, va, field));
                  return false;
               }

          } catch (Exception e) {
             errors.add(field.getKey(),Resources.getActionError (request, va, field));
             return false;
          }

       }
       return true;

    }


Thanks& Regards,
VimalSagar
vimal.y@gmail.com






Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Validation Framework display of user input dayakarb Struts 0 April 20th, 2007 02:31 AM
validation dhaval229 ASP.NET 1.0 and 1.1 Professional 2 February 14th, 2006 02:10 PM
validation dsmportal Pro VB.NET 2002/2003 4 February 10th, 2005 12:26 PM
Validation koneruvijay VS.NET 2002/2003 2 January 20th, 2004 08:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.