Back Forum Reply New

Checking Form errors at onSubmit()

Hi, I'm in the onSubmit() of my SimpleFormController. I receive my object, and the user has selected quot;Addquot;, fine. Ok, now I have to INSERT into the database a new record. But that record code already exists! I want to fire an error like the same way the validator does, and keep the user on the same form, so he can change the field with the already-existing code. I don't want to put all this logic on the validator, because in theory, the validator should check other things (like agegt;=0 and things like that, no?)  If I return null, the user gets a blank page  If I regturn this.getFormView(), I get the already existing record, loosing what the user has typed, and without the posibility to display the error.  How you people deal with this? Thanks,

Hi Dani,

If you're waiting till you get to onSubmit(request, response, obj, errors) to check the errors, you make it very hard for yourself to bind the existing values to the form, add your error to the Errors/BindException object and redirect to the original form.

Ideally you ought to check this in the validation stage. You've got a few options here:

- you can either do this from the validator object,
- you can override onBindAndValidate and add the code there, so that the DB access calls are initiated from the controller class.
- you can call setValidator(Validator) in the controller's constructor, and specify an anonymous inner class which can then share the controller class' DAO/Service object.

There's not really a one-size-fits all solution. I guess it boils down to whether you think a validator should simply check whether parameters are sematically acceptable, or whether it should do more and perform DB checks.

I call into my DAO layer from several validators. By doing it this way you can get the most out of the framework itself (ie you wind up writing less code).

Any use?

Cheers

Mike

Thanks, I guess that onBindAndValidate() was the method I was looking for!
¥
Back Forum Reply New