• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validation not working - any ideas

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am using a customer validate() method in my ActionForm extension to validate a simple form but it does not work for me.

Can anyone let me know the steps I need to carry out to enable this so I can see if I have missed anything?

Thanks

BTW - my validate method is:

 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Have you specified the messages "shadow.error.username" and
"shadow.error.password" in Application.resource file. If not then even though validation method is being called, you won't see the error.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
form fields submitted aren't null...
they're empy strings.

Make some kind of helper utility to ease these tests, as in:

if (FormUtil.isTrivial(username)) {
errors.add("login", new ActionMessage("shadow.error.username"));
}

you can then overload it for all the form objects you have.

In your example, username may be empty, but not null.
 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Thanks for replying.

Yes. I have the following lines in my Resources.properties file:

shadow.error.username=Please enter your username
shadow.error.password=Please enter your password

More information:

struts-config.xml contains:

<form-beans>
<form-bean name="loginForm" type="forms.LoginForm"/>
</form-beans>

<action-mappings>
<action path="/login"
type="actions.LoginAction"
name="loginForm"
input="/login.jsp"
scope="session"
validate="true">
<forward name="success" path="/login.jsp"/>
<forward name="failure" path="/login.jsp"/>
</action>
</action-mappings>

Login.jsp contains:



I was expecting the errors to appear where <html:errors/> is...
 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andrew Yahner:
form fields submitted aren't null...
they're empy strings.

Make some kind of helper utility to ease these tests, as in:

if (FormUtil.isTrivial(username)) {
errors.add("login", new ActionMessage("shadow.error.username"));
}

you can then overload it for all the form objects you have.

In your example, username may be empty, but not null.



That was it - doh

Thankyou for your help.

I changed the test to:

 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew is correct. A textfield left empty on a form is still submitted as "", or empty string.

Your check should include:
if(username == null || "".equals(username)){
// add error
...
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marc Peabody:
Andrew is correct. A textfield left empty on a form is still submitted as "", or empty string.

Your check should include:
if(username == null || "".equals(username)){
// add error
...



oops, you beat me to it
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think,

if(username == null || username.length() == 0)


It's faster than ....
 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a reason I would check for a null value on the fields using struts?

The only access to my ActionForm is from my login form so presumably there could never be a case where where the values are null. Or have I missed something in my newbie struts endeavours?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic