You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Paolo Beccari <pb...@email.it> on 2007/05/19 17:29:48 UTC

[S2] field-validator regex ignoring given message

Hi all,
I'm a S2 newbie.
Trying to validate input in a *-validation.xml, and found a strange 
behaviour:

<validators>
    <field name="id">
        <field-validator type="required">
            <message key="required"/>
        </field-validator>
        <field-validator type="regex">
            <!--param name="regex">[0-9]</param-->
            <param name="expression">[0-9]</param>
            <!--message key="requirednumber"/-->
            <message>id is not a numeric value</message>
        </field-validator>
    </field>
</validators>

When i test validation, the "required" key is shown correctly (taken from 
package.properties file), but instead of "requirednumber" key (as shown 
above I tried with a plain message too), i'm always receiving a "Invalid 
field value for field "id"." message.

Someone can help?
P.


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


Re: [S2] field-validator regex ignoring given message

Posted by Lance <la...@marketpipe.com>.
Ah... pls ignore

Paolo Beccari wrote:
>
>
>
>> <param name="expression">[0-9]*(\.[0-9]+)?</param>
>
> Hi Lance, as already established (see previous posts), it is not a 
> matter of regular expressions.
> The matter is: the variable in the Action is a Long (and MUST be a Long).
> The regex validation does not work, if the variable is not a String.
> I'm searching a way to validate (through Action-validation.xml) a 
> field that must be numeric, and that is defined as a Long in the 
> action corresponding to the Form. According to this, field-validator 
> type="int" is inadequate, and I was not able to found a number (or 
> Long?) validation.
>
> P.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


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


Re: [S2] field-validator regex ignoring given message

Posted by Paolo Beccari <pb...@email.it>.


> <param name="expression">[0-9]*(\.[0-9]+)?</param>

Hi Lance, as already established (see previous posts), it is not a matter of 
regular expressions.
The matter is: the variable in the Action is a Long (and MUST be a Long).
The regex validation does not work, if the variable is not a String.
I'm searching a way to validate (through Action-validation.xml) a field that 
must be numeric, and that is defined as a Long in the action corresponding 
to the Form. According to this, field-validator type="int" is inadequate, 
and I was not able to found a number (or Long?) validation.

P.



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


Re: [S2] field-validator regex ignoring given message

Posted by Lance <la...@marketpipe.com>.
<param name="expression">[0-9]*(\.[0-9]+)?</param>


Paolo Beccari wrote:
>
>> --- Paolo Beccari <pb...@email.it> wrote:
>>>   <field-validator type="regex">
>>>    <param name="expression">[0-9]</param>
>>>    <message key="requirednumber"/>
>>>         </field-validator>
>>>     </field>
>>> </validators>
>>
>> --- "Dave Newton" <ne...@yahoo.com> wrote:
>> If it's defined as a Long in your action then you
>> might not be able to run a regex on it--I would
>> imagine type conversion has already happened.
>>
>> d.
>>
>
> Thank for the reply, Dave. I switched to regex because I wanted to 
> validate a field to be a number, but in the docs I found only 
> type="int" validation while I need a wider range. So how to validate a 
> Long value? I'm pretty sure it's possible...
>
> Thanks again.
> P.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


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


Re: [S2] field-validator regex ignoring given message

Posted by Paolo Beccari <pb...@email.it>.
>
>>> Paolo Beccari <pb...@email.it> wrote:
>>> I suppose this message is always returned when a
>>> conversion error occurs.
>>> Next step is finding a way to hide it. I'll post the
>>> solution, if ever find one.
>>
>> "Dave Newton" <ne...@yahoo.com> wrote:
>> $ find . -name "*.properties" | xargs grep -i invalid
>> ./src/java/com/opensymphony/xwork2/xwork-messages.properties:xwork.default.invalid.fieldvalue=Invalid
>> field value for field "{0}"
>>
>> Definitely coming from the XWork conversion process.
>>
>> Don't know offhand how to fix it, but there's some
>> proof, at least.
>>
>> d.
>
> Solved. It can be workarounded using i18n Action configuration.
>
> Action.properties file:
> #custom message
> #invalid.fieldvalue.idATsro=custom message for conversion error
> #no message
> #note that it is well-formatted in html rendering (no additional spaces in 
> page before package.properties messages displays)
> invalid.fieldvalue.idATsro=
>

Sorry, forgot the reference:
http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/util/AnnotationXWorkConverter.html

(it is even possible to define a custom conversion through a 
Action-conversion.properties file... Very interesting...)

HTH
P.


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


Re: [S2] field-validator regex ignoring given message

Posted by Dave Newton <ne...@yahoo.com>.
--- Paolo Beccari <pb...@email.it> wrote:
> >> Paolo Beccari <pb...@email.it> wrote:
> >> I suppose this message is always returned when a
> >> conversion error occurs.
> > "Dave Newton" <ne...@yahoo.com> wrote:
>
./src/java/com/opensymphony/xwork2/xwork-messages.properties:xwork.default.invalid.fieldvalue=Invalid
> > field value for field "{0}"
> >
> > Definitely coming from the XWork conversion
> process.
> Solved. It can be workarounded using i18n Action
> configuration.
> 
> Action.properties file:
> invalid.fieldvalue.idATsro=

That's great; thanks for posting the followup!

Dave



       
____________________________________________________________________________________Ready for the edge of your seat? 
Check out tonight's top picks on Yahoo! TV. 
http://tv.yahoo.com/

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


Re: [S2] field-validator regex ignoring given message

Posted by Paolo Beccari <pb...@email.it>.
>> Paolo Beccari <pb...@email.it> wrote:
>> I suppose this message is always returned when a
>> conversion error occurs.
>> Next step is finding a way to hide it. I'll post the
>> solution, if ever find one.
>
> "Dave Newton" <ne...@yahoo.com> wrote:
> $ find . -name "*.properties" | xargs grep -i invalid
> ./src/java/com/opensymphony/xwork2/xwork-messages.properties:xwork.default.invalid.fieldvalue=Invalid
> field value for field "{0}"
>
> Definitely coming from the XWork conversion process.
>
> Don't know offhand how to fix it, but there's some
> proof, at least.
>
> d.

Solved. It can be workarounded using i18n Action configuration.

Action.properties file:
#custom message
#invalid.fieldvalue.idATsro=custom message for conversion error
#no message
#note that it is well-formatted in html rendering (no additional spaces in 
page before package.properties messages displays)
invalid.fieldvalue.idATsro=

P.


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


Re: [S2] field-validator regex ignoring given message

Posted by Dave Newton <ne...@yahoo.com>.
--- Paolo Beccari <pb...@email.it> wrote:
> I suppose this message is always returned when a
> conversion error occurs.
> Next step is finding a way to hide it. I'll post the
> solution, if ever find one.

$ find . -name "*.properties" | xargs grep -i invalid
./src/java/com/opensymphony/xwork2/xwork-messages.properties:xwork.default.invalid.fieldvalue=Invalid
field value for field "{0}"

Definitely coming from the XWork conversion process.

Don't know offhand how to fix it, but there's some
proof, at least.

d.



       
____________________________________________________________________________________Be a better Heartthrob. Get better relationship answers from someone who knows. Yahoo! Answers - Check it out. 
http://answers.yahoo.com/dir/?link=list&sid=396545433

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


Re: [S2] field-validator regex ignoring given message

Posted by Paolo Beccari <pb...@email.it>.
>> Paolo Beccari <pb...@email.it> wrote:
>> So how to validate a Long value?
>
> "Dave Newton" <ne...@yahoo.com> wrote:
> You might just be able to use the conversion
> validator.
>
> http://struts.apache.org/2.x/docs/conversion-validator.html
>
> I have not tried this, so I'd be interested in the
> results if you do!
>
> Thanks,
> Dave
>

Just tested. Result is (uh, almost) Ok.

The new Action-validation.xml is:
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
    <field name="idATsro">
        <field-validator type="required">
   <message key="requiredstring"/>
        </field-validator>
  <field-validator type="conversion">
   <message key="requirednumber"/>
   <!--message>${idATsro} is not a numeric value</message-->
        </field-validator>
    </field>
</validators>

package.properties file is:
requiredstring = ${getText(fieldName)} is required.
requirednumber = ${getText(fieldName)} should be a number.

idATsro is a Long variable in Action.java.

Test case:

input: NULL
message: idATsro is required. [taken from package.properties: OK]

input: a number
message: None (FORM Submitted)

input: a character, or a string
message:
Invalid field value for field "idATsro". [SO, WHERE DO YOU COME FROM?]
+
idATsro is required. [taken from package.properties: OK]
+
idATsro should be a number. [taken from package.properties: OK]

The only thing that I want to remove (because i did not configured it in 
package.properties file) is the message:
Invalid field value for field "idATsro"

I suppose this message is always returned when a conversion error occurs.
Next step is finding a way to hide it. I'll post the solution, if ever find 
one.

HTH
P.




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


Re: [S2] field-validator regex ignoring given message

Posted by Paolo Beccari <pb...@email.it>.


> --- Paolo Beccari <pb...@email.it> wrote:
>> So how to validate a Long value?
> 
> You might just be able to use the conversion
> validator.
> 
> http://struts.apache.org/2.x/docs/conversion-validator.html
> 
> I have not tried this, so I'd be interested in the
> results if you do!
> 
> Thanks,
> Dave
> 

I'm going to try it in a few days (maybe tomorrow) and post results to ML.
P.


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


Re: [S2] field-validator regex ignoring given message

Posted by Dave Newton <ne...@yahoo.com>.
--- Paolo Beccari <pb...@email.it> wrote:
> So how to validate a Long value?

You might just be able to use the conversion
validator.

http://struts.apache.org/2.x/docs/conversion-validator.html

I have not tried this, so I'd be interested in the
results if you do!

Thanks,
Dave



       
____________________________________________________________________________________Get the free Yahoo! toolbar and rest assured with the added security of spyware protection.
http://new.toolbar.yahoo.com/toolbar/features/norton/index.php

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


Re: [S2] field-validator regex ignoring given message

Posted by Paolo Beccari <pb...@email.it>.
> --- Paolo Beccari <pb...@email.it> wrote:
>>   <field-validator type="regex">
>>    <param name="expression">[0-9]</param>
>>    <message key="requirednumber"/>
>>         </field-validator>
>>     </field>
>> </validators>
>
> --- "Dave Newton" <ne...@yahoo.com> wrote:
> If it's defined as a Long in your action then you
> might not be able to run a regex on it--I would
> imagine type conversion has already happened.
>
> d.
>

Thank for the reply, Dave. I switched to regex because I wanted to validate 
a field to be a number, but in the docs I found only type="int" validation 
while I need a wider range. So how to validate a Long value? I'm pretty sure 
it's possible...

Thanks again.
P.



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


Re: [S2] field-validator regex ignoring given message

Posted by Dave Newton <ne...@yahoo.com>.
--- Paolo Beccari <pb...@email.it> wrote:
>   <field-validator type="regex">
>    <param name="expression">[0-9]</param>
>    <message key="requirednumber"/>
>         </field-validator>
>     </field>
> </validators>

If it's defined as a Long in your action then you
might not be able to run a regex on it--I would
imagine type conversion has already happened.

d.



       
____________________________________________________________________________________Boardwalk for $500? In 2007? Ha! Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.
http://get.games.yahoo.com/proddesc?gamekey=monopolyherenow  

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


Re: [S2] field-validator regex ignoring given message

Posted by Paolo Beccari <pb...@email.it>.
>>
>> Hi all,
>> I'm a S2 newbie.
>> Trying to validate input in a *-validation.xml, and found a strange
>> behaviour:
>>
>> <validators>
>>     <field name="id">
>>         <field-validator type="required">
>>             <message key="required"/>
>>         </field-validator>
>>         <field-validator type="regex">
>>             <!--param name="regex">[0-9]</param-->
>>             <param name="expression">[0-9]</param>
>>             <!--message key="requirednumber"/-->
>>             <message>id is not a numeric value</message>
>>         </field-validator>
>>     </field>
>> </validators>
>>
>> When i test validation, the "required" key is shown correctly (taken from
>> package.properties file), but instead of "requirednumber" key (as shown
>> above I tried with a plain message too), i'm always receiving a "Invalid
>> field value for field "id"." message.
>>
>> Someone can help?
>> P.
>
>
> Hi Paolo, I'm a newbie as well, but I'm going to try to answer this one
> since I've run into a similar problem. The validation seems to happen in 
> the
> order that is specified in the xml file. My mistake was that I left id
> specified in the request parameters, but since it was a post form 
> submission
> I needed to included in the actual form (so that the properties in the
> action get set accordingly). So just check that you have an id field in 
> your
> form (usually a hidden field).
>
> Hope that helps.
>
> Will
>
I did some testing, and here is the result:

Here is the Action-validation.xml file:

VALIDATOR:

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
    <field name="idATsro">
        <field-validator type="required">
   <message key="requiredstring"/>
        </field-validator>
  <field-validator type="regex">
   <param name="expression">[0-9]</param>
   <message key="requirednumber"/>
        </field-validator>
    </field>
</validators>

Then, if I define idATsro as a String in the Action corresponding to the 
form (as defined in struts.xml):

input: NULL
message (use message defined in package.properties): idATsro is required.

input: "a"
message (use message defined in package.properties): idATsro should be a 
number.

input: 21
message (use message defined in package.properties): idATsro should be a 
number. This is due to [0-9] regex.

input: 2
message: none (FORM Submitted)


If I define id as a Long in the Action (and it IS a Long, in my purpose):

NULL
message (use message defined in package.properties): idATsro is required.

input: "a"
message:
Invalid field value for field "idATsro" (I dont' know where it comes from. 
It's not defined by me)
+
idATsro is required (defined in package.properties)

input: 21
message: none (FORM Submitted)

input: 2
message: (FORM Submitted)

So, it seems that if I define the field as a String in the Action, things 
are working properly. If I define the field as a Long (and it's what I 
need), strange behaviour is occurring.

Where am I wrong? There is a logical explanation?
Many Thanks
P.


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


Re: [S2] field-validator regex ignoring given message

Posted by Will Smith <bu...@gmail.com>.
On 5/19/07, Paolo Beccari <pb...@email.it> wrote:
>
> Hi all,
> I'm a S2 newbie.
> Trying to validate input in a *-validation.xml, and found a strange
> behaviour:
>
> <validators>
>     <field name="id">
>         <field-validator type="required">
>             <message key="required"/>
>         </field-validator>
>         <field-validator type="regex">
>             <!--param name="regex">[0-9]</param-->
>             <param name="expression">[0-9]</param>
>             <!--message key="requirednumber"/-->
>             <message>id is not a numeric value</message>
>         </field-validator>
>     </field>
> </validators>
>
> When i test validation, the "required" key is shown correctly (taken from
> package.properties file), but instead of "requirednumber" key (as shown
> above I tried with a plain message too), i'm always receiving a "Invalid
> field value for field "id"." message.
>
> Someone can help?
> P.


Hi Paolo, I'm a newbie as well, but I'm going to try to answer this one
since I've run into a similar problem. The validation seems to happen in the
order that is specified in the xml file. My mistake was that I left id
specified in the request parameters, but since it was a post form submission
I needed to included in the actual form (so that the properties in the
action get set accordingly). So just check that you have an id field in your
form (usually a hidden field).

Hope that helps.

Will