0

This question is more about a good practice to avoid code duplication when validating input data.

The application imports a spreadsheet (XLS or XLSX) and instantiates an OrderList Object, which contains multiple OrderUnits (referring to each row of the original spreadsheet). The user is able to alter some properties, also those, which have not been set via the import spreadsheet. In other words the import feature forms a base of data, which will then be extended by the user. Some properties even are set automatically (e.g. if the length of an orderUnit is greater than 5.0 metres, the material will be set to steel, instead of default plastic.). The is a json configuration file, where all properties of an orderunit can be defined, including fields like type (string, decimal, list) and validation (not_blank,...).

To my question: Because there are two methods of inserting data, where is a good place to validate the data, before the orderUnit property gets set?

  1. One place is the import of data, which completely takes place in my OrderUnit model class I could use some custom validation mechanism.

  2. The other place is via the JTextComponents which can be set or altered after the import has taken place. I would like to use InputVerifier on every JComponent.

But this leads to duplicate code of my validation creteria.

Is there a nice way to validate once or at least reuse the same validation model twice?

ILCAI
  • 1,164
  • 2
  • 15
  • 35
  • Why not just moving your validation methods to a new (utility) class? – André Stannek Jan 20 '15 at 08:24
  • Why don't you validate everything in the GUI using InputVerifier. The import from the XLS can go through the JTextComponents, so you catch bad data coming from there, too. – laune Jan 20 '15 at 08:37
  • @laune, that seems practical, the problem is that not every imported cell has a corresponding JTextComponent, but well its own validation. i am wondering if this feels good, but at least much better than the duplication. André: thank you, perhaps this seems to be a good and scalable option. I was just wondering if there is a JValidator or something similiar, which you should use for those (common?) cases. – ILCAI Jan 20 '15 at 08:54
  • You can always add some "blind" JTextComps that aren't added to a frame. – laune Jan 20 '15 at 08:56

0 Answers0