Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 Annotations validate String using RegexFieldValidator

 
Alvaro Lopez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use Struts2 Annotations to validate some fields of a bean.
In this case, I would like to validate a phone number String with a regex.

Googling aroung I found the @RegexFieldValidator annotation, so I included in my class and made some tests:



However, any regex expression seemed to work. I tried even with "\\d{9}" and "[0-9]{9}".
Even with type=ValidatorType.SIMPLE and:


The Required StringValidator works properly...What´s going on here?
Is it possible to use this validator properly?

Thanks in advance
 
Yogesh Lonkar
Ranch Hand
Posts: 94
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using this expression="\\d{3}-\\d{3}-\\d{4}"
 
Alvaro Lopez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still accepts any value. It seems to be a problem with @RegexFieldValidator... :S

I used:

In the AccountAction, I introduced some Validations:



And the account phone is being set in a JSP through:

 
Yogesh Lonkar
Ranch Hand
Posts: 94
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please provide your action class code? because @RegexFieldValidator(message="Invalid Phone Number",expression="\\d{3}-\\d{3}-\\d{4}") works perfectly.
 
Alvaro Lopez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I´m sorry but it doesn´t work. I tried directly with a field in the Action class, not in the Account Bean class and it accepts any string as phone2 value

Let´s see the case, just with the phone2 field In NewAccount.jsp I have:



In AccountActionClass.java:



In the JSP I just fill the field phone2 with any string (figures or letters). While the other fields show an error message as they are null, the phone2 label is black and it seems to be correct.

So..what am I doing wrong?

Thanks in advance.
 
Yogesh Lonkar
Ranch Hand
Posts: 94
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK here is how i do it.
Model Class


Action class

and this should do it for @VisitorFieldValidator link to Docs
 
Alvaro Lopez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Struts 2.3.12.

Maybe is it compulsory to use javax.persistence package in order @VisitorFieldValidator to work? I bet it isn't.

I have tried some more tests but @RegexFieldValidator doesn't work properly .
It sets the String (in account.phone or in phone2) but it doesn't use the Regex to validate it, so this annotation is not working for me.

However, @RequiredStringValidator works perfectly for both fields. If I don't write anything, the error message appears!

I suppose I have to validate the phone number in another way...maybe with @IntRangeFieldValidator

Thanks for your kind help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic