cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grzegorz Kossakowski <>
Subject Re: [Cocoon 2.2 + Cforms] Validation a field using the Databas e
Date Wed, 07 Nov 2007 23:01:04 GMT
Raphaël Piéroni pisze:
> Hi,
> I have fixed my issue by modifying the widget value
> from the flowscript (in a catch) when the database complains.
> ...
>  var form = new Form(...);
>  form.createBinding(...);
>  form.load(binding);
>  while (invalid) {
>    form.showForm(...);
>    try {
>      database.createObject(binding);// throws exception
>      invalid=false;
>    catch (ex if (ex.javaException instanceof InvalidNameException)) {
>            // InvalidNameException is a java exception
>      var error = new ValidationError('name.already.exist', false); //
> also tried with true
>      var nameWidget = form.lookupWidget('name');
>      nameWidget.setValue(''); // the widget is correclty reseted on the
> output
>      nameWidget.setValidationError(error);
>    }
>  }
>  cocoon.showPage(...);
> ...
> But i don't manage to have my error message displayed.
> Any help is welcome.

This is because form (and its widgets) is marked as valid even if you set validation error.
whole problem comes from the fact that you break processing sequence by trying to set validation
error after forms processing is finished.

The solution is pretty simple: you should implement custom validator[1] and add[2] it to the
you need *before* you call form.showForm(). If you wonder how to get an access to database
custom validator the answer is also quite simple, you just need to implement it as component
implementing WidgetValidator interface and obtain it in flowscript by using cocoon.getComponent().

Thinking about it more, I recall that Giacomo migrated Forms to Spring recently making it
to implement custom validators as Spring-beans. That means you can just implement your validator
add reference to it in your Form definition as explained here[3] with the remark that you
should use following construct:
<fd:java ref="com.mycompany.validators.MyCustomValidator"/>

instead of:
<fd:java class="com.mycompany.validators.MyCustomValidator"/>

when in the ref the bean name should be put.

This change is not documented and it would be a _very_ good idea to bug Giacomo (who introduced
change) to provide migration guide for users migrating from Forms 1.0.x to 1.1.x. Oh yes,
Springified Forms block will be released as 1.1.0.



Grzegorz Kossakowski
Committer and PMC Member of Apache Cocoon

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message