You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Vilya Harvey <vi...@lineone.net> on 2004/06/16 16:19:16 UTC

Validate an XML document in a flowscript

Hi all,

I'm using CForms and the binding framework to edit an XML document. That's 
working great. What I'd like to do is validate the document against an XML 
Schema after it's been updated with the values from the form. How can I 
achieve this?

The flowscript which controls the form at the moment looks like this (copied 
heavily from the cocoon samples):

	function enterForecasts(form)
	{
	  // parse the document to a DOM-tree
	  var documentObj = loadDocument("sampledata.xml");

	  // bind the document data to the form
	  form.load(documentObj);
	  // show the form to the user until it is validated successfully
	  form.showForm("form-display-pipeline");
	  // bind the form's data back to the document
	  form.save(documentObj);

	  // save the DOM-tree back to an XML file, the makeTargetURI
	  // function makes a modified filename so that the
	  // original document is not overwritten
	  saveDocument(documentObj, makeTargetURI(documentURI));

	  cocoon.sendPage("form-success-pipeline");
	}

What I'd like to be able to do is insert some code after the form.save(...) 
line which validates the form, and loops back to the form.load(...) line if 
validation fails. Any suggestions?

Thanks in advance,

Vil.
-- 
             __
    o|   _. /  \|o._  _  _ ._  _  ._  _ _|_
\/ ||\/(_|| (|/||| |(/_(_)| |(/_o| |(/_ |_
      /     \__
http://website.lineone.net/~vilya


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


Re: Validate an XML document in a flowscript

Posted by Vilya Harvey <vi...@lineone.net>.
Vilya Harvey wrote:
> I'm sorry I didn't make it clear, but it's actually the code to perform 
> the validation that I'm looking for. I have no problem writing loops in 
> JavaScript! Thanks for the reply anyway,

Should have mentioned, but this is what I've tried so far:

==================
function ValidationErrorHandler_handleError(error) {
   this.errorsFound = true;
}

function ValidationErrorHandler() {
   this.errorsFound = false;
   this.handleError = ValidationErrorHandler_handleError;
}

function validateDocument(documentObj) {
   var handler = new ValidationErrorHandler();
   var docConfig = documentObj.domConfig;

   docConfig.setParameter("error-handler", handler);
   docConfig.setParameter("validate", true);
   documentObj.normalizeDocument();

   return handler.errorsFound;
}
==================

The ValidationErrorHandler class is supposed to be an implementation of the
This fails with a TYPE_MISMATCH_ERR message on the line which sets the 
handler on the docConfig object. Presumably that's because 
ValidationErrorHandler doesn't explicitly implement the DOMErrorHandler 
interface, but I don't know JavaScript well enough to know how to make it 
implement that interface.

Initially I tried just calling docConfig.setParameter("validate", true) 
followed by the call to normalizeDocument(). That works, in so far as the 
document does get validated against the schema, but there doesn't seem to be 
a way of finding out whether validation succeeded or not. I was hoping an 
exception would be thrown if validation failed, but instead it just fails 
silently in the absence of an error handler.

So that's where I'm at now. I guess I could implement the 
ValidationErrorHandler in java; that'll be the next step. Any other 
suggestions would be great though!
-- 
             __
    o|   _. /  \|o._  _  _ ._  _  ._  _ _|_
\/ ||\/(_|| (|/||| |(/_(_)| |(/_o| |(/_ |_
      /     \__
http://website.lineone.net/~vilya

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


Re: Validate an XML document in a flowscript

Posted by Vilya Harvey <vi...@lineone.net>.
Hi,

Simon Hutchinson wrote:
> Vilya Harvey wrote:
> 
>  > What I'd like to be able to do is insert some code after the
>  > form.save(...) line which validates the form, and loops back to the
>  > form.load(...) line if validation fails. Any suggestions?
> 
> 
> Sounds like a simple while loop might do the trick.

I'm sorry I didn't make it clear, but it's actually the code to perform the 
validation that I'm looking for. I have no problem writing loops in 
JavaScript! Thanks for the reply anyway,

Vil.
-- 
             __
    o|   _. /  \|o._  _  _ ._  _  ._  _ _|_
\/ ||\/(_|| (|/||| |(/_(_)| |(/_o| |(/_ |_
      /     \__
http://website.lineone.net/~vilya

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


Re: Validate an XML document in a flowscript

Posted by Simon Hutchinson <si...@misgl.com>.
Vilya Harvey wrote:

 > What I'd like to be able to do is insert some code after the
 > form.save(...) line which validates the form, and loops back to the
 > form.load(...) line if validation fails. Any suggestions?


Sounds like a simple while loop might do the trick.

var isValid = false;

function enterForecasts(form)
     {
       // parse the document to a DOM-tree
       var documentObj = loadDocument("sampledata.xml");

       // bind the document data to the form

       while(!isValid) {
       	form.load(documentObj);
      	 // show the form to the user until it is validated successfully
       	form.showForm("form-display-pipeline");
       	// bind the form's data back to the document
       	form.save(documentObj);

	validateForm(form);
       }

       // save the DOM-tree back to an XML file, the makeTargetURI
       // function makes a modified filename so that the
       // original document is not overwritten
       saveDocument(documentObj, makeTargetURI(documentURI));

       cocoon.sendPage("form-success-pipeline");
}

function validateForm(form) {
	//pseudo code
			
	//do validation stuff
	if(validates) {
	   isValid=true;		
	}
	
	return;
}


Cheers

Si
 
 
 
 
 
-- 
 
Merlin Information Systems Limited, 
 
Merlin House, Gawcott Road, Buckingham, United Kingdom. MK18 1TN
 
Tel: +44 (0) 1280 824331  Fax: +44 (0) 1280 824112
 

 
http://www.misgl.com
 
Provider of IT Services and Online Portal Support Services. 
 
 
 
Confidentiality:
 
The information contained in this email (including any attachments) is confidential and is intended solely for the use of the named addressee. Access, copying or re-use of the information in it by any other person is not authorised. If you are not the intended recipient, please notify us immediately by telephone or by e-mail to admin@misgl.com
 

 
 
 
*** This mail has been scanned for viruses ***
 

 

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