Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

share validator class between to forms problem

Status
Not open for further replies.

gwu

MIS
Dec 18, 2002
239
US
I have two jsp forms in which I want to share the same validator class. The problem: form1 uses all three form inputs (firstName,lastName,email) but form2 only uses two form inputs (firstName,lastName). form1 works fine but form2 will give me an error because it is expecting an email input. What is the best way to test for the email input so as to skip the email validation code?

thanks in advance

Code:
import java.util.*;
public class FormBean {
  private String firstName;
  private String lastName;
  private String email;
  private Hashtable errors;

  public boolean validate() {
    boolean allOk=true;
    if (firstName.equals("")) {
      errors.put("firstName","Please enter your first name");
      firstName="";
      allOk=false;
    }
    if (lastName.equals("")) {
      errors.put("lastName","Please enter your last name");
      lastName="";
      allOk=false;
    }
    if (email.equals("") || (email.indexOf('@') == -1)) {
      errors.put("email","Please enter a valid email address");
      email="";
      allOk=false;
    }
    return allOk;
  }

  public String getErrorMsg(String s) {
    String errorMsg =(String)errors.get(s.trim());
    return (errorMsg == null) ? "":errorMsg;
  }

  public FormBean() {
    firstName="";
    lastName="";
    email="";
    errors = new Hashtable();
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public String getEmail() {
    return email;
  }

  public void setFirstName(String fname) {
    firstName =fname;
  }

  public void setLastName(String lname) {
    lastName =lname;
  }

  public void setEmail(String eml) {
    email=eml;
  }

  public void setErrors(String key, String msg) {
    errors.put(key,msg);
  }
}
 

I will pas a value to the function Validate() example form1 or form2 depending which form and then before you do the email validation check to see if you have form1 validate otherwise don't.
 
As an alternative, I would probably do this sort of validation client-side using javascript. Failing that, find a java implementation of an HTML Form object and use that.

Greg.
 
grega: I am trying this because I dont want to use javascript anymore!! ;) Can you show me an example of "java implementation of an HTML Form object"?

4345487: I should have mentioned this before but I could have more forms. I wanted to create a dynaminc validator which any form can use. I could do what you are suggesting but I would have to keep adding the different fomrs and it would get complex..what do you think?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top