You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Eric Shannon <es...@careersite.com> on 2006/02/01 05:43:58 UTC

CForm Persistence Question

I have a form that upon submit gets persisted via an EJB call to our 
business layer.

I need to improve my current implementation so that if an error occurs 
in the business layer,  the error is displayed on the form giving the 
user the chance to possibly make changes and re-submit the form.

I can use java script validation in the binding to implement _most 
_validation logic.

How can I persist the form  with the possibility of needing to set a 
ValidationError?

I have tried using <fb:save-form>, but the form always completes even if 
I set an error on a widget and return false.
I have also tried using java script validation on the form, but from 
what I can tell the form data is not bound, so I do not have a valid 
object to persist.

Thanks for any ideas,

Eric

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: CForm Persistence Question

Posted by Jason Johnston <co...@lojjic.net>.
Eric Shannon wrote:
> 
> I have a form that upon submit gets persisted via an EJB call to our
> business layer.
> 
> I need to improve my current implementation so that if an error occurs
> in the business layer,  the error is displayed on the form giving the
> user the chance to possibly make changes and re-submit the form.
> 
> I can use java script validation in the binding to implement _most
> _validation logic.
> 
> How can I persist the form  with the possibility of needing to set a
> ValidationError?
> 
> I have tried using <fb:save-form>, but the form always completes even if
> I set an error on a widget and return false.
> I have also tried using java script validation on the form, but from
> what I can tell the form data is not bound, so I do not have a valid
> object to persist.


This is a great question!  There are of course many ways you could
approach this, but here's one that I've found works well:

First of all, don't worry about preventing your form from
"completing"... you can re-display the form even after it has exited the
showForm() loop by simply calling showForm() again on the same Form
object, for instance within a while-loop.  This makes it easy to perform
validations in addition to the field-level validation specified in the
form definition.  Think of two "levels" of validation: the field-level
validations done by the form, and business-level validation performed by
your business layer and handled by flow outside the showForm() loop.

To communicate the business-level validation errors up to the UI, let
your business layer throw an exception of a particular type you create
(for instance a ValidationException) when it encounters a validation
error.  This exception object will carry the message that should be
presented to the user.  Then in your flowscript (I assume you're using
flowscript here... javaflow would work similarly) use a try/catch to
handle the exception and redisplay the form with the message.  For
instance, off the top of my head:


function myFlow() {
  var form = new Form("...");
  form.createBinding("...");

  var finished = false;
  while(!finished) {
    form.showForm("...");
    var dataObj = new DataObject(); //whatever
    form.save(dataObj);

    try {
      callTheEJB(dataObj); //make the ejb call
      //if we got here the ejb call completed without an exception.
      finished = true;
    }
    catch(e) {
      if(e instanceof ValidationException) {
        //display the exception's message in a <fd:messages> widget:
        form.lookupWidget("messagesWidget").addMessage(e.getMessage());
      }
      else throw e; //let other exceptions bubble up
    }
  }

  // we're done, show confirmation...
}


I hope that gives you a good start.
--Jason





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org