cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tobia Conforto <tobia.confo...@gmail.com>
Subject Re: Cocoon Forms 2.1 - One of two fields required
Date Wed, 25 Feb 2009 15:09:08 GMT
Wolfgang Ruelfing wrote:
> Hello,
> you can put the general form validation code straight into the form  
> element

Thank you!  This is exactly what I was looking for.

I had tried to do something like this on my own, but I made an  
critical mistake: I set the validation error on the form itself,  
instead of one of its children, and it didn't work. Your solution  
works very well.

Here are a couple of improvements: I don't set the "composite" error  
if any of the fields already have a more basic error (because I don't  
want to override it with mine); and whenever the state of this  
"composite" validation error changes, I push both fields on to the  
user (otherwise I would get stale error icons on the browser, using  
Ajax.)  Can you please comment on my implementation?  Is there a  
simpler solution?


<form xmlns="http://apache.org/cocoon/forms/1.0#definition">
<on-create>
   <javascript>

     widget.setAttribute('phonemobile_valid', true);

   </javascript>
</on-create>
<validation>
   <javascript><![CDATA[

     var phone = widget.lookupWidget('/phone'),
         mobile = widget.lookupWidget('/mobile'),
         old_valid = widget.getAttribute('phonemobile_valid'),
         new_valid = true;

     /* we require the user to enter his landline and/or mobile phone;
      * if both fields are without a valid value AND without a  
temporary,
      * invalid value, set an error on both
      */
     if (! phone.getValue() && ! mobile.getValue()
         && ! phone.getValidationError()
         && ! mobile.getValidationError()) {
       var message = "Please enter your landline and/or mobile phone";
       phone.setValidationError(new ValidationError(message, false));
       mobile.setValidationError(new ValidationError(message, false));
       new_valid = false;
     }

     /* if this composite validation state has changed since last time,
      * push both fields to the browser
      */
     if (old_valid != new_valid) {
       widget.addWidgetUpdate(phone);
       widget.addWidgetUpdate(mobile);
     }

     widget.setAttribute('phonemobile_valid', new_valid);
     return new_valid;

   ]]></javascript>
</validation>
<widgets>
   ...


-Tobia

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


Mime
View raw message