You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Greg <gr...@gmail.com> on 2005/02/24 16:22:18 UTC

Validation with java

Hello, 

I have been using cocoon 2.1.4 with tomcat 4 to develop some web
forms. I need to run a checksum alogorithm on a value entered into a
widget and would like to do this using java as opposed to javascript.
I am, however, unsure how to reference a java class from the
validation area of the widget definition. I would appreciate any help
on this bearing in mind I must pass parameters and get a boolean
return parameter from the invoked method.

G Farnan.

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


Re: Validation with java

Posted by Scott Yeadon <sc...@anu.edu.au>.
The validation appears to only support Javascript, however there's a 
simple way around this:

Create a JavaClassValidatorBuilder which operates the same way as the 
ListenerBuilder:

package my.package;

import java.lang.Exception;
import org.apache.cocoon.forms.formmodel.WidgetDefinition;
import org.apache.cocoon.forms.validation.WidgetValidatorBuilder;
import org.apache.cocoon.forms.validation.WidgetValidator;
import org.apache.cocoon.forms.util.DomHelper;
import org.apache.cocoon.util.ClassUtils;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.w3c.dom.Element;

public class JavaClassValidatorBuilder implements 
WidgetValidatorBuilder, ThreadSafe
{
   public WidgetValidator build(Element element,
                                 WidgetDefinition widget) throws Exception
   {
       String name = DomHelper.getAttribute(element, "class");

       Object validator = ClassUtils.newInstance(name);
       if (Class.forName(WIDGET_VALIDATOR_PACKAGE + PACKAGE_SEPARATOR + 
WIDGET + 
WIDGET_VALIDATOR_CLASSNAME).isAssignableFrom(validator.getClass()))
       {
           return (WidgetValidator)validator;
       }
       else
       {
           throw new Exception("Class " + validator.getClass() + " is 
not a " + WIDGET_VALIDATOR_CLASSNAME);
       }
   }
}

Where:
WIDGET_VALIDATOR_PACKAGE is "org.apache.cocoon.forms.validation"
PACKAGE_SEPARATOR is "."
WIDGET_VALIDATOR_CLASSNAME is "WidgetValidator"

Update the coccon.xconf file's forms-validators element with:
<validator class="my.package.JavaClassValidatorBuilder" name="java"/>

Restart cocoon and works fine, you can now add tagging such as
<fd:validation>
   <java class="my.other.package.MyJavaClassThatValidatesStuff"/>
</fd:validation>

Scott.

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


Jorg Heymans wrote:

>
> Greg wrote:
>
>> Hello,
>> I have been using cocoon 2.1.4 with tomcat 4 to develop some web
>> forms. I need to run a checksum alogorithm on a value entered into a
>> widget and would like to do this using java as opposed to javascript.
>> I am, however, unsure how to reference a java class from the
>> validation area of the widget definition. I would appreciate any help
>> on this bearing in mind I must pass parameters and get a boolean
>> return parameter from the invoked method.
>
>
> Did you know about the validator interface [1]? It is referenced near 
> the top on page [2].
>
> Hope this helps you on your way.
>
> Regards
> Jorg
>
> [1]http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/forms/validation/WidgetValidator.html 
>
> [2]http://cocoon.apache.org/2.1/userdocs/forms/validation.html
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>

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


Re: Validation with java

Posted by Jorg Heymans <jh...@domek.be>.
Greg wrote:
> Hello, 
> 
> I have been using cocoon 2.1.4 with tomcat 4 to develop some web
> forms. I need to run a checksum alogorithm on a value entered into a
> widget and would like to do this using java as opposed to javascript.
> I am, however, unsure how to reference a java class from the
> validation area of the widget definition. I would appreciate any help
> on this bearing in mind I must pass parameters and get a boolean
> return parameter from the invoked method.

Did you know about the validator interface [1]? It is referenced near 
the top on page [2].

Hope this helps you on your way.

Regards
Jorg

[1]http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/forms/validation/WidgetValidator.html
[2]http://cocoon.apache.org/2.1/userdocs/forms/validation.html


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