jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes Validating forms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validating forms" Watch "Validating forms" New topic
Author

Validating forms

janice ang
Greenhorn

Joined: Jan 03, 2007
Posts: 5
I've tried to validate my fields with the below codes. However, the error message does not show up. Why is that so?

public class RegistrationFormBean extends org.apache.struts.action.ActionForm {

private String email;

public String getEmail() {
return (email);
}

public void setEmail(String email) {
this.email = email;
}

private String password;

public String getPassword() {
return (password);
}

public void setPassword(String password) {
this.password = password;
}

private String password2;

public String getPassword2() {
return (password2);
}

public void setPassword2(String password2) {
this.password2 = password2;
}

private String name;

public String getName() {
return (name);
}

public void setName(String name) {
this.name = name;
}


private String randomAssignTeam;

public String getRandomAssignTeam() {
return (randomAssignTeam);
}

public void setRandomAssignTeam(String randomAssignTeam) {
this.randomAssignTeam = randomAssignTeam;
}

private String seekMembers;

public String getSeekMembers() {
return (seekMembers);
}

public void setSeekMembers(String seekMembers) {
this.seekMembers = seekMembers;
}

private String term;

public String getTerm() {
return (term);
}

public void setTerm(String term) {
this.term = term;
}

private String skills;

public String getSkills() {
return (skills);
}

public void setSkills(String skills) {
this.skills = skills;
}

private String remarks;

public String getRemarks() {
return (remarks);
}

public void setRemarks(String remarks) {
this.remarks = remarks;
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getEmail() == null || getEmail().length() < 1) {
errors.add("email", new ActionMessage("error.Registration.email.required"));
}
if (getEmail() != null && getEmail().indexOf('@')==-1) {
errors.add("emailValidity", new ActionMessage("error.Registration.email.notvalid"));
}
if (getName() == null || getName().length() < 1) {
errors.add("studentName", new ActionMessage("error.Registration.name.required"));
}
if (getPassword() == null || getPassword().length() < 1 || getPassword2() == null || getPassword2().length() < 1 || !getPassword().equals(getPassword2())) {
errors.add("password", new ActionMessage("error.Registration.password.required"));
}
if (getTerm() == null || getTerm().length() < 1) {
errors.add("term", new ActionMessage("error.Registration.term.required"));
}

return errors;
}

public void reset() {
this.email = null;
this.password = null;
this.password2 = null;
this.name = null;
this.randomAssignTeam = null;
this.term = null;
this.skills = null;
this.remarks = null;
}

}

In my Message Properties, I've included:

error.Registration.email.required=Please enter the email address.
error.Registration.name.required=Please enter the name.
error.Registration.password.required=Both password field is required. Make sure their value are the same.
error.Registration.email.notvalid=Please use a valid email address.
error.Registration.term.notvalid=Please select the term you want to take IS480.


Anywhere gone wrong?
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
have you added <html:errors/> in your jsp (wherever you need to display the errors if thats what you want)? this may be the reason..otherwise please specify what errors you're getting if any.
[ April 09, 2007: Message edited by: Christian Nash ]

- Christian
janice ang
Greenhorn

Joined: Jan 03, 2007
Posts: 5
Hi,

yup, i'v added <html:error/> when i click on the submit button, it only display:
-
-
-
-
-

that's all on the registration page. i think it recognized my fields but it just fail to display the error message.
[ April 09, 2007: Message edited by: janice ang ]
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
the tag should be <html:errors/> and not <html:error/>..if you still not able to display please give your jsp and struts config file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating forms
 
Similar Threads
Error: creating bean of class (HELP THE NEWBE)
Cannot retrieve mapping for action
javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean: "org.apache.struts
struts
confusing compile error