Form Validation - Computations

Problem: I am using a an “audit” type field to ensure accuracy of data entered. the example is:
Total:
A:
B:
C:
D:

The validation that is required is: Total = A + B + C

The object which is used in the bean tracks the total and the individual values. It has a validateCount routine as well.

The form object looks like (for give the cleansing, so it may not compile as is):


public class MyForm extends Form {
	private static final long serialVersionUID = -2420461223721163962L;
	private static final List<String> fields = 
		Arrays.asList(new String[] {"total","A","B","C","D"});

	private BeanItem<ObjectType> obj;
	
	public MyForm()
	{
		setCaption("Details");
		setSizeFull();
		setImmediate(true);
		setWriteThrough(true);
		setInvalidCommitted(false);						
	}

	public void setMetric(BeanItem<ObjectType> obj) {
		this.obj= obj;
		setItemDataSource(obj);
		this.setVisibleItemProperties(fields);
	}

	public BeanItem<ObjectType> getObj() {
		return obj;
	}
}

As you can see there is no FieldFactory as the fields are very simple.

In the layout that contains the form, I have the form as well as buttons that get added below the form. I have a buttonClick listener which looks like:


	public void buttonClick(ClickEvent event) {
		if (discard.equals(event.getButton()))
		{
			form.discard();
		}
		else if (submit.equals(event.getButton()))
		{
            try {  
           		form.commit();
           		DBUtils.saveObj(obj);
            } catch (Exception e) {
            	// Ingnored, we'll let the Form handle the errors                }
            }						
		}
	}

I don’t have any JPA/Spring interaction as I’ve never worked with it… I’m going to be going there at some point, but for now I’m doing the hack of a util class dealing with writing the objects to the DB. Yes, I know that’s not right :slight_smile:

As a side note: Should save/cancel buttons be added as part of the form footer or should they be added below the form in the layout?

Any thoughts?

Thanks.