Reputation: 3742
I have this abstract Form Class courtesy of BalusC that sets validation messages
public abstract class Form {
// Properties
private Map messages = new LinkedHashMap();
private List formFeedback = new ArrayList();
private List errorMessages = new ArrayList();
private boolean hasError;
Then I have a bunch of Classes that extend Form
public class ParentForm extends Form {
public class SubForm1 extends Form {
public class SubForm2 extends Form {
public class SubForm3 extends Form {
in the parentForm I call each subform but I can not figure out how to pass the validation messages up from the subforms to the parent.
public class ParentForm extends Form {
public Object update(HttpServletRequest request) {
pseudo code
//if subform1 is being edited
SubForm1 subForm1 = new SubForm1 (authenticatedUser);
subFormBean1 sfb1 = new subFormBean1 ();
sfb1 = subForm1.update(request);
//if subform2 is being edited
SubForm2 subForm2 = new SubForm2 (authenticatedUser);
subFormBean2 sfb2 = new subFormBean2 ();
sfb2 = subForm2.update(request);
The parent Form
service is executed in a servlet using a Struts style Action
public class UpdateAction implements ControllerAction {
private Form form;
private Object obj;
private HashMap modelMap;
public UpdateAction(HashMap modelMap, Form form, Object obj) {
this.form = form;
this.obj = obj;
this.modelMap = modelMap;
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
obj = form.update(request);
request.setAttribute("obj", obj);
request.setAttribute("form", form);
In my jsps I'd put
<span class="formError"><c:out value="${form.messages.whatever}" />
but this is picking up messages only from the parent form, not from any subforms
I'm wondering if something like `form[subForm].messages.whatever would work? Something like
if (!sfb1.notSuccess) {
parentForm.setSubFormMessages(sfb1.getMessages());
}
My project is kind of far a long but basically I want sections (components??) on an HTML page to be HTML forms that are active only under certain conditions (its a work flow application for checklists we used to route around the office by hand) Wonder if it is better to suck it up and port over to JSF or modify the Form class.
Edit I'm trying to implement the abstract subForm but now something like this on a subForm will still update the Dao even though say my entry validate (processEntry) failed
processEntry(request, ad);
processEdit_date(request, ad);
if (isSucces()) {
dao.update(ad, authenticatedUser);
}
Upvotes: 0
Views: 214
Reputation: 1108712
You should basically pass the ParentForm
during construction of the subform.
SubForm1 subForm1 = new SubForm1(this, authenticatedUser);
with
public SubForm1(Form parent, User authenticatedUser) {
this.parent = parent;
this.authenticatedUser = authenticatedUser;
}
and then in the SubForm1
delegate the messaging to it
public addMessage(field, message) {
parent.addMessage(field, message);
}
To keep SubForm1
, SubForm2
, etc DRY, you could create another abstract class following the wrapper pattern:
public abstract class SubForm extends Form {
private Form parent;
public SubForm(Form parent) {
this.parent = parent;
}
public addMessage(field, message) {
parent.addMessage(field, message);
}
// ...
}
And then design SubForm1
, SubForm2
, etc as follows:
public class SubForm1 extends SubForm {
public SubForm1(Form parent, User authenticatedUser) {
super(parent);
this.authenticatedUser = authenticatedUser;
}
// ...
}
Upvotes: 1