You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Raphaël Piéroni <ra...@aql.fr> on 2007/11/07 17:13:38 UTC

Re: [Cocoon 2.2 + Cforms] Validation a field using the Databas e

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.

Regards,

Raphaël

Raphaël Piéroni a écrit :
> Hi guys,
>
> I have a cform which works well.
> The flowscript (ECMA) that handles the cform delegates
> to a spring bean to create an object in database.
> But when that objet already exist it throws an exception.
>
> I had managed to catch the exception in the flow script.
> But i would like to know if (and how) to perform a validation
> of the nonexistence of the object i try to create before calling
> my spring beans. for this purpose i can add a check method on
> the spring beans.
> But how do i give the beans from the flow script
> to the cform for it to use in validation? How to i define the
> validation to perform in the cform?
>
> If there is some doco on the subjet, pleas'e redirect me to the doco.
>
>
> Best regards,
>
>
> Raphaël
>
>
> ---------------------------------------------------------------------
> 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: [Cocoon 2.2 + Cforms] Validation a field using the Databas e

Posted by Raphaël Piéroni <ra...@aql.fr>.
My fault i use a internal boolean to check, but used the inversed 
condition before adding the error.
It is working now.

Raphaël

Raphaël Piéroni a écrit :
> Grzegorz Kossakowski a écrit :
>> Raphaël Piéroni pisze:
>>   
>>> Thanks for the answer.
>>>
>>> Is this functionnality avaliable in 1.0.0-RC1 ? It seems not.
>>>
>>> When that new version wil be released ?
>>>     
>>
>> As I pointed out, Spring-based Forms will be released as 1.1.0. I don't know what are current plans
>> and if there any show-stoppers for 1.1.0's first milestone release. I guess it's better to ask on
>> dev list. Hint: usually pleasant bugging developers to push the release can give good result. ;)
>>
>>   
> Meanwhile having the springified validator, i added them myself in the 
> flowscript
> like in :
>         var form = new Form("view/user/create.cform");
>         var validator = spring.getBean("createUserValidator");
>         form.lookupWidget('userName').addValidator(validator);
>         form.lookupWidget('unixLogin').addValidator(validator);
>
> In the validator i do :
>                 ValidationError error = new 
> ValidationError("error.createUser.userName", true);
>                 ((Field) widget).setValidationError(error);
>                 return false;
>
> I saw (using logs) that the validation is false (which is what i test)
> But the form completes as if it was validated.
>
> Raphaël
>
>> Anyway, you can still develop Avalon-based validator using 1.0.0-RC1 version of Forms. The choice is
>> yours.
>>
>>   
>
>
> --------------------------------------------------------------------- 
> 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: [Cocoon 2.2 + Cforms] Validation a field using the Databas e

Posted by Grzegorz Kossakowski <gk...@apache.org>.
Raphaël Piéroni pisze:
> Thanks for the answer.
> 
> Is this functionnality avaliable in 1.0.0-RC1 ? It seems not.
> 
> When that new version wil be released ?

As I pointed out, Spring-based Forms will be released as 1.1.0. I don't know what are current plans
and if there any show-stoppers for 1.1.0's first milestone release. I guess it's better to ask on
dev list. Hint: usually pleasant bugging developers to push the release can give good result. ;)

Anyway, you can still develop Avalon-based validator using 1.0.0-RC1 version of Forms. The choice is
yours.

-- 
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


Re: [Cocoon 2.2 + Cforms] Validation a field using the Databas e

Posted by Grzegorz Kossakowski <gk...@apache.org>.
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