commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aaron Sheffey <AShef...@dtcc.com>
Subject Re: [Validator] Commons Validator problem
Date Tue, 30 Aug 2005 17:05:11 GMT
No - you don't need to add anything to the custom validator method. 
However, in whatever code you are using to parse your validation results 
and display the return messages, you need to 1) grab the custom message 
from the field, if there is one, and if not, then to 2) grab the message 
from the default list in the properties file.  Here's an example:

<> indicates pseudo-code

String message = <the ValidatorResult of the current 
property>.getField().getMsg( <the ValidatorAction grabbed from the Map of 
the current propery's actions> );
try
{
        // If there was no message, grab the default message from the 
global resource
        if ( message == null ) message = <ResourceBundle>.getString( 
action.getMsg() );
}
        catch ( Exception e )
{
        // log the resource exception here - you should have a message for 
every validator method
}




David Duff <david.duff@planetzorbo.co.uk> 
08/30/2005 12:50 PM
Please respond to
"Jakarta Commons Users List" <commons-user@jakarta.apache.org>


To
Jakarta Commons Users List <commons-user@jakarta.apache.org>
cc

Subject
Re: [Validator] Commons Validator problem






Hi,

Thanks for all replies up to now and your patience :).

Here is the validateMinLength method in my custom validator

public static boolean validateMinLength(Object bean, Field field)
       {
        // Get value that the user has entered.
        String value =
            ValidatorUtils.getValueAsString(bean, field.getProperty());
 
        // Get the minlength var value from validation.xml
        String minLength = field.getVarValue("minlength");

 // Convert value to int for minLength() method.
        int minValue = Integer.parseInt(minLength);
 
        // Compare value to min length value allowed.
        boolean result = GenericValidator.minLength(value,minValue);
        return result;
    }

I realise that a should be using the formatInt() method to convert the
string to int but if you could ignore that for now.

Should I be reading the resource and name attributes from args and msg
in this method and setting them within a message class to get them work
within my application?

Thanks in advance,
David

Aaron Sheffey wrote:

>We output custom messages all the time with no issue, as follows:
>
><field property="some_field_name" depends="mask">
>        <arg key="some_field_name"/>
> <var><var-name>mask</var-name><var-value>^some_mask$</var-value></
>var>
>        <msg name="mask" key="Some Field Name must be \'some_mask\'." 
>resource="false" />
></field>
>
>
>
>
>David Duff <david.duff@planetzorbo.co.uk> 
>08/30/2005 12:14 PM
>Please respond to
>"Jakarta Commons Users List" <commons-user@jakarta.apache.org>
>
>
>To
>Jakarta Commons Users List <commons-user@jakarta.apache.org>
>cc
>
>Subject
>Re: [Validator] Commons Validator problem
>
>
>
>
>
>
>Hi,
>
>Not using struts, using commons-validator.
>
>David
>
>Niall Pemberton wrote:
>
> 
>
>>The message handling will be in your custom validator - so without 
seeing
>>how thats working its impossible to know why this isn't working for you.
>>
>>Are you using this with Struts? If so, does the *standard* struts 
>> 
>>
>minlength
> 
>
>>validator work properly?
>>
>>Niall
>>
>>----- Original Message ----- 
>>From: "David Duff" <david.duff@planetzorbo.co.uk>
>>Sent: Tuesday, August 30, 2005 4:52 PM
>>
>>
>>
>>
>> 
>>
>>>Hi
>>>
>>>I am baffled to why the name and resource attributes in the arg and msg
>>>seem not to work in my application.  Surely someone has used validator
>>>with a second argument, overridden the default message, or set resource
>>>to false in their configuration.  I was thinking that the problem I 
have
>>>was of my making.
>>>
>>>Can someone please tell me why these attributes won't work on my
>>>application? All the information that you will need is below:
>>>
>>>Thanks in advance,
>>>David
>>>
>>>David Duff wrote:
>>>
>>>
>>>
>>> 
>>>
>>>>Hi,
>>>>
>>>>That doesn't work for me.  It keeps printing out the message I defined
>>>>in validation-rules.xml minus the second arg because it is not defined
>>>>anymore.
>>>>
>>>><validator
>>>>     name="minlength"
>>>>classname="com.company.product.controller.validator.CustomValidator"
>>>>     method="validateMinLength"
>>>>     methodParams="java.lang.Object,
>>>>                   org.apache.commons.validator.Field"
>>>>     *msg="errors.min"*/>
>>>>
>>>>Is there any way to override this message with the one in 
>>>> 
>>>>
>validator.xml?
> 
>
>>>><msg name="minlength" key="My Message" resource="false" />
>>>>
>>>>Thanks in advance,
>>>>DAvid
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>Aaron Sheffey wrote:
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> 
>>>>
>>>>>If you want to print out a specific message for the validation, you 
>>>>> 
>>>>>
>can
> 
>
>>>>>add that to the field, too:
>>>>>
>>>>><field property="username" depends="required,minlength">
>>>>>   <arg key="username"/>
>>>>>   <var><var-name>minlength</var-name><var-value>6</var-value></var>
>>>>>     <msg name="minlength" key="The minimum length for the User Name
>>>>>
>>>>>
>>>>> 
>>>>>
>>is
>>
>>
>> 
>>
>>>>>6" resource="false" />
>>>>></field>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>David Duff <david.duff@planetzorbo.co.uk>
>>>>>08/24/2005 12:09 PM
>>>>>Please respond to
>>>>>"Jakarta Commons Users List" <commons-user@jakarta.apache.org>
>>>>>
>>>>>
>>>>>To
>>>>>Jakarta Commons Users List <commons-user@jakarta.apache.org>
>>>>>cc
>>>>>
>>>>>Subject
>>>>>Re: Commons Validator problem
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>Hi,
>>>>>
>>>>>Thanks for the reply
>>>>>
>>>>>The msg I want to print out has two arguments:
>>>>>
>>>>>errors.min=The {0} must have at least {1} characters.
>>>>>
>>>>>This would leave:
>>>>>
>>>>>The user name must have at least {1} characters.
>>>>>
>>>>>I can get round the problem I'm having by specifying a value in the
>>>>>resource like
>>>>>
>>>>><field property="username" depends="required,minlength">
>>>>>           <arg key="user.username" position="0"/>
>>>>>           <arg key="user.min" position="1"/>
>>>>>
>>>>><var><var-name>minlength</var-name><var-value>6</var-value></var>
>>>>>
>>>>></field>
>>>>>
>>>>>properties file:
>>>>>user.min= 6
>>>>>
>>>>>I would like to do it like it was specified in the user guides 
instead
>>>>>of intoducing a workaround and having to input the value twice.  Once


>>>>> 
>>>>>
>in
> 
>
>>>>>the resource and another in the validator.xml.
>>>>>
>>>>>Thanks in advance.
>>>>>David
>>>>>
>>>>>Aaron Sheffey wrote:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> 
>>>>>
>>>>>>You probably don't need to specify all the other stuff in the field.
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> 
>>>>>>
>>>>>Based
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> 
>>>>>
>>>>>>on what I have done with mask, do something like
>>>>>>
>>>>>><field property="username" depends="required,minlength">
>>>>>>    <arg key="username"/>
>>>>>><var><var-name>minlength</var-name><var-value>6</var-value></var>
>>>>>></field>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>David Duff <david.duff@planetzorbo.co.uk>
>>>>>>08/24/2005 11:43 AM
>>>>>>Please respond to
>>>>>>"Jakarta Commons Users List" <commons-user@jakarta.apache.org>
>>>>>>
>>>>>>
>>>>>>To
>>>>>>commons-user@jakarta.apache.org
>>>>>>cc
>>>>>>
>>>>>>Subject
>>>>>>Commons Validator problem
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>Hi,
>>>>>>
>>>>>>I am trying to specify a min. length for a user name field in my
>>>>>>application.
>>>>>>
>>>>>>In my validatior.xml I have specified:
>>>>>>
>>>>>>|<field property="username" depends="required,minlength">
>>>>>><arg key="user.username" position="0"/>
>>>>>><arg name="minlength" key="${var:minlength}" resource="false" ||
>>>>>>position="1"||/>
>>>>>><var><var-name>minlength</var-name><var-value>6</var-value></var>
>>>>>></field>
>>>>>>
>>>>>>In my validator-rules.xml I have:
>>>>>>
>>>>>><validator
>>>>>>    name="minlength"
>>>>>>classname="com.company.product.controller.validator.CustomValidator"
>>>>>>    method="validateMinLength"
>>>>>>    methodParams="java.lang.Object,
>>>>>>                  org.apache.commons.validator.Field"
>>>>>>    msg="errors.min"/>
>>>>>>
>>>>>>In my custom validator I have my validateMinLength() method.
>>>>>>
>>>>>>I would expect the two arguments (user.username and minlength) to

get
>>>>>>picked up but it only picks up the first argument even though ||I

can
>>>>>>see the two args in the field object||.  When I remove the name
>>>>>>attribute from arg, it will then pick up the second argument but 
look
>>>>>>for the key in the resource even although resource is set to false.
>>>>>>
>>>>>>I am using commons-validator-1.1.4.jar|
>>>>>>
>>>>>>Can anyone tell me what I'm doing wrong?
>>>>>>
>>>>>>Thanks in advance.
>>>>>>David
>>>>>>
>>>>>>
>>>>>> 
>>>>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
>>
>>
>>
>> 
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>
> 
>
>------------------------------------------------------------------------
>
>No virus found in this incoming message.
>Checked by AVG Anti-Virus.
>Version: 7.0.344 / Virus Database: 267.10.17/84 - Release Date: 
29/08/2005
> 
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message