You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by co...@apache.org on 2004/01/31 00:53:25 UTC

cvs commit: cocoon-2.1/src/blocks/woody/samples/v2 woody_flow_example.js

coliver     2004/01/30 15:53:25

  Modified:    src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2
                        Form.js ScriptableWidget.java
               src/blocks/woody/samples/v2 woody_flow_example.js
  Log:
  Added support for flowscript validation of forms
  
  Revision  Changes    Path
  1.3       +19 -14    cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2/Form.js
  
  Index: Form.js
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2/Form.js,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Form.js	29 Jan 2004 16:36:17 -0000	1.2
  +++ Form.js	30 Jan 2004 23:53:25 -0000	1.3
  @@ -69,10 +69,9 @@
           formMgr = cocoon.getComponent(FormManager.ROLE);
           resolver = cocoon.getComponent(SourceResolver.ROLE);
           src = resolver.resolveURI(uri);
  -        this.form = formMgr.createForm(src);
  +        var form = formMgr.createForm(src);
           this.binding = null;
  -        this.eventHandler = null;
  -        this.formWidget = new Widget(this.form);
  +        this.formWidget = new Widget(form);
           this.local = cocoon.createPageLocal();
       } finally {
           cocoon.releaseComponent(formMgr);
  @@ -99,10 +98,6 @@
   
   /**
    * Manages the display of a form and its validation.
  - *
  - * On return, the calling code can check some properties to know the form result :
  - * - "isValid" : true if the form was sucessfully validated
  - *
    * @parameter uri the page uri (like in cocoon.sendPageAndWait())
    */
   
  @@ -110,13 +105,23 @@
       var FormContext = Packages.org.apache.cocoon.woody.FormContext;
       this.local.webContinuation = cocoon.createWebContinuation();
       // this is needed by the WoodyTemplateTransformer:
  -    this.formWidget["woody-form"] = this.formWidget.unwrap();
  -    cocoon.request.setAttribute("woody-form", this.form);
  +    var javaWidget = this.formWidget.unwrap();;
  +    this.formWidget["woody-form"] = javaWidget;
  +    cocoon.request.setAttribute("woody-form", javaWidget);
       var wk = cocoon.sendPageAndWait(uri, this.formWidget, fun);
       var formContext = 
  -        new FormContext(cocoon.request, this.form.getLocale());
  -    this.isValid = this.form.process(formContext);
  -    if (!this.isValid) {
  +        new FormContext(cocoon.request, javaWidget.getLocale());
  +    var userErrors = 0;
  +    this.formWidget.validationErrorListener = function(widget, error) {
  +        if (error != null) {
  +            userErrors++;
  +        }
  +    }
  +    var finished = javaWidget.process(formContext);
  +    if (this.onValidate) {
  +        this.onValidate(this);
  +    }
  +    if (!finished || userErrors > 0) {
           this.redisplay();
       }
       return wk;
  @@ -157,12 +162,12 @@
       if (this.binding == null) {
           throw new Error("Binding not configured for this form.");
       }
  -    this.binding.loadFormFromModel(this.form, object);
  +    this.binding.loadFormFromModel(this.formWidget.unwrap(), object);
   }
   
   Form.prototype.save = function(object) {
       if (this.binding == null) {
           throw new Error("Binding not configured for this form.");
       }
  -    this.binding.saveFormToModel(this.form, object);
  +    this.binding.saveFormToModel(this.formWidget.unwrap(), object);
   }
  
  
  
  1.5       +21 -0     cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2/ScriptableWidget.java
  
  Index: ScriptableWidget.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2/ScriptableWidget.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ScriptableWidget.java	30 Jan 2004 19:19:53 -0000	1.4
  +++ ScriptableWidget.java	30 Jan 2004 23:53:25 -0000	1.5
  @@ -502,8 +502,29 @@
               } else {
                   ((Field)delegate).setValidationError(validationError);
               }
  +            formWidget.notifyValidationErrorListener(this, validationError);
           }
       }
  +
  +    private void notifyValidationErrorListener(ScriptableWidget widget,
  +                                               ValidationError error) {
  +        Object fun = getProperty(this, "validationErrorListener");
  +        if (fun instanceof Function) {
  +            try {
  +                Scriptable scope = getTopLevelScope(this);
  +                Scriptable thisObj = scope;
  +                Context cx = Context.getCurrentContext();
  +                Object[] args = new Object[2];
  +                args[0] = widget;
  +                args[1] = error;
  +                ((Function)fun).call(cx, scope, thisObj, args);
  +            } catch (Exception exc) {
  +                throw Context.reportRuntimeError(exc.getMessage());
  +            }
  +        }
  +    }
  +
  +
   
       public Widget jsFunction_unwrap() {
           return delegate;
  
  
  
  1.3       +14 -0     cocoon-2.1/src/blocks/woody/samples/v2/woody_flow_example.js
  
  Index: woody_flow_example.js
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/v2/woody_flow_example.js,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- woody_flow_example.js	30 Jan 2004 19:21:35 -0000	1.2
  +++ woody_flow_example.js	30 Jan 2004 23:53:25 -0000	1.3
  @@ -142,6 +142,16 @@
       wid.drinks.value = ["Jupiler", "Coca Cola"];
   
       //
  +    // You can do additional validation of a form in your flowscript by
  +    // assigning a function to the form's 'onValidate' property:
  +    //
  +    form.onValidate = function() {
  +        if (wid.cowheight.value == 3) {
  +            wid.cowheight.setValidationError("cowheight cannot be 3");
  +        }
  +    }
  +
  +    //
       // You can set additional properties on any widget that will
       // be accessible in the pipeline (e.g. with JXTemplateGenerator)
       //
  @@ -153,6 +163,10 @@
       //
       form.showForm("form1-display-pipeline");
       print("cowheight = "+wid.cowheight.value);
  +    if (wid.cowheight.value == 2) {
  +      wid.cowheight.setValidationError("cowheight cannot be 2");
  +      form.redisplay();
  +    }
   }
   
   function selectCar() {