cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grzegorz Kossakowski <gkossakow...@apache.org>
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(...);
>    form.save(binding);
>    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.
The
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
field
you need *before* you call form.showForm(). If you wonder how to get an access to database
from
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
possible
to implement custom validators as Spring-beans. That means you can just implement your validator
and
add reference to it in your Form definition as explained here[3] with the remark that you
(probably)
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
this
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.

HTH.

[1]
http://cocoon.apache.org/2.2/blocks/forms/1.0/apidocs/org/apache/cocoon/forms/validation/WidgetValidator.html
[2]
http://cocoon.apache.org/2.2/blocks/forms/1.0/apidocs/org/apache/cocoon/forms/formmodel/AbstractWidget.html#addValidator(org.apache.cocoon.forms.validation.WidgetValidator)
[3] http://cocoon.apache.org/2.2/blocks/forms/1.0/484_1_1.html

-- 
Grzegorz Kossakowski
Committer and PMC Member of Apache Cocoon
http://reflectingonthevicissitudes.wordpress.com/

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


Mime
View raw message