1

I have an MVC application where the Controller starts a Thread and therein starts a progressbar inside the View the action originated and calls the Model to go over a list of data selected in a View and process it in some way.

Now while the Model is going over the data it encounters one or more entries that he wants the user to confirm for some reason. How should this be handled using MVC?
Note that the Controller can handle multiple views, which of them should be responsible for the user input, how to differentiate between them, ... ?

jb10210
  • 1,158
  • 5
  • 15

1 Answers1

2

Validating input should be handled at the earliest opportunity possible in the view. The view can query the model to verify an entry in context. As a concrete example, this InputVerifier overrides verify() to ensure numeric entry, but it might also ask the model do additional checks, perhaps comparing against other model attributes. For example,

@Override
public boolean verify(JComponent input) {
    try {
        value = Double.parseDouble(field.getText());
        return model.isValid(value); // also check model
    } catch (NumberFormatException e) {
        return false;
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • OK but what if it is not possible to validate the input directly, in my case for example the validation happens on a server over RMI with a bulk of input values, it is not possible to first loop over the input in the view or controller. – jb10210 Oct 17 '13 at 11:21
  • 1
    It sounds like your "earliest opportunity possible" is after submitting the entire form. You'll either have to require the server to offer finer grained verification or make a best effort to highlight fields that failed to verify. – trashgod Oct 17 '13 at 11:32
  • Thanks, I actually came up with the same answer when posting the comment :) – jb10210 Oct 17 '13 at 11:43