001: package web.examples;
002:
003: import static org.jreform.criteria.Criteria.emailAddress;
004:
005: import java.util.Date;
006:
007: import javax.servlet.http.HttpServletRequest;
008:
009: import org.jreform.Group;
010: import org.jreform.HtmlForm;
011: import org.jreform.Input;
012: import org.jreform.Radio;
013: import org.jreform.Select;
014:
015: /**
016: * A credit card application form.
017: */
018: public class CreditCardAppForm extends HtmlForm {
019: private static final EmploymentStatusDataType employmentType = new EmploymentStatusDataType();
020:
021: private HttpServletRequest request;
022:
023: @SuppressWarnings("unchecked")
024: public CreditCardAppForm(HttpServletRequest request) {
025: this .request = request;
026:
027: radio(stringType(), "title");
028: input(stringType(), "firstName");
029: input(stringType(), "lastName");
030: input(dateType("dd-MM-yyyy"), "dob");
031:
032: input(stringType(), "address");
033: input(stringType(), "city");
034: radio(stringType(), "ownOrRent");
035: input(stringType(), "email", emailAddress());
036: input(stringType(), "phoneNumber");
037:
038: // uses a custom InputDataType
039: select(employmentType, "employmentStatus");
040:
041: Group employer = optionalGroup("employer");
042: employer.input(stringType(), "company");
043: employer.input(stringType(), "businessPhoneNumber");
044:
045: radio(booleanType(), "hasAccountWithUs");
046:
047: Group account = optionalGroup("accountDetails");
048: account.select(stringType(), "accountType");
049: account.input(intType(), "accountNumber");
050: account.input(intType(), "branchNumber");
051:
052: input(intType(), "monthlyIncome");
053: input(intType(), "monthlyExpenses");
054:
055: }
056:
057: /**
058: * Additional validation:
059: *
060: * If user is Employed, check that employment details were provided.
061: * If user is a current customer, check that account details were provided.
062: */
063: @Override
064: protected void additionalValidate() {
065: EmploymentStatus empStatus = getInputValue("employmentStatus");
066: Group employerInfo = getGroup("employer");
067:
068: if (empStatus == EmploymentStatus.EMPLOYED) {
069: employerInfo.setRequired(true);
070: employerInfo.validate(request);
071: }
072:
073: Group accountDetails = getGroup("accountDetails");
074:
075: if (getHasAccountWithUs().getValue()) {
076: accountDetails.setRequired(true);
077: accountDetails.validate(request);
078: }
079: }
080:
081: //
082: // Getters
083: //
084:
085: public Radio<String> getTitle() {
086: return getRadio("title");
087: }
088:
089: public Input<String> getFirstName() {
090: return getInput("firstName");
091: }
092:
093: public Input<String> getLastName() {
094: return getInput("lastName");
095: }
096:
097: public Input<Date> getDob() {
098: return getInput("dob");
099: }
100:
101: public Input<String> getAddress() {
102: return getInput("address");
103: }
104:
105: public Input<String> getCity() {
106: return getInput("city");
107: }
108:
109: public Radio<String> getOwnOrRent() {
110: return getRadio("ownOrRent");
111: }
112:
113: public Input<String> getEmail() {
114: return getInput("email");
115: }
116:
117: public Input<String> getPhoneNumber() {
118: return getInput("phoneNumber");
119: }
120:
121: public Select<EmploymentStatus> getEmploymentStatus() {
122: return getSelect("employmentStatus");
123: }
124:
125: public Input<String> getCompany() {
126: return getInput("company");
127: }
128:
129: public Input<String> getBusinessPhoneNumber() {
130: return getInput("businessPhoneNumber");
131: }
132:
133: public Radio<Boolean> getHasAccountWithUs() {
134: return getRadio("hasAccountWithUs");
135: }
136:
137: public Input<String> getAccountType() {
138: return getInput("accountType");
139: }
140:
141: public Input<Integer> getAccountNumber() {
142: return getInput("accountNumber");
143: }
144:
145: public Input<Integer> getBranchNumber() {
146: return getInput("branchNumber");
147: }
148:
149: public Input<Integer> getMonthlyIncome() {
150: return getInput("monthlyIncome");
151: }
152:
153: public Input<Integer> getMonthlyExpenses() {
154: return getInput("monthlyExpenses");
155: }
156:
157: public EmploymentStatus[] getEmploymentStatusValues() {
158: return EmploymentStatus.values();
159: }
160:
161: }
|