Hi Tobia
I use the following construct to do that:
After all relevant form fields I add a form field (in my case
id="form-validation") that is not displayed in the form but invokes a
javascript validation function. The javascript functions gets two
parameters: the widget and an "ignoreList". The ignoreList contains blank
separated field names that will not be tested.
Have a look ... Raffaele
<fd:widgets>
<fd:field id="..." required="false">
...
</fd:field>
<fd:field id="form-validation">
<fd:datatype base="string"/>
<fd:validation>
<fd:javascript>
return setValidationErrorIfAllCformFieldsAreEmpty(widget,
"form-validation status");
</fd:javascript>
</fd:validation>
</fd:field>
</fd:widgets>
<ft:form-template id="..." action="#{$cocoon/continuation/id}.continue"
method="post">
<ft:widget id=".."/>
<ft:widget id="form-validation"/>
</ft:form-template>
function setValidationErrorIfAllCformFieldsAreEmpty (widget, ignoreList)
{
var debug = false;
if (debug) {
java.lang.System.out.println("--- enter
setValidationErrorIfAllCformFieldsAreEmpty() ---");
java.lang.System.out.println("widget[" + widget.getName() +
"]");
}
var form = widget.getForm();
var list = form.getChildren();
var elem;
var widgetName;
while (list.hasNext()) {
elem = list.next();
widgetName = elem.getName();
if (debug) { java.lang.System.out.println("processing widget[" +
widgetName + "]"); }
var exp = "\\b" + widgetName + "\\b";
var reg = new RegExp(exp, "g");
if (reg.test(ignoreList)) {
if (debug) { java.lang.System.out.println("> field is in
ignore list // continue"); }
continue;
}
if (!elem.isValid()) {
if (debug) { java.lang.System.out.println("> field is no
valid // retrun false"); }
return false;
}
if (elem.getValue() != null) {
if (debug) { java.lang.System.out.println("> field has a
value // retrun true"); }
return true;
}
}
if (debug) { java.lang.System.out.println("> processing finished //
all fields empty // set validation error // return false"); }
var valError = new
org.apache.cocoon.forms.validation.ValidationError("allFieldsAreEmpty");
widget.setValidationError(valError);
return false;
}
-----Ursprüngliche Nachricht-----
Von: Tobia Conforto [mailto:[email protected]]
Gesendet: Montag, 16. Februar 2009 18:23
An: [email protected]
Betreff: Cocoon Forms 2.1 - One of two fields required
Hello
In Cocoon 2.1, Forms, how do I require the user to fill at least one
out of two (or more) fields?
For example, I might have the 'phone' and 'mobile' fields. I want the
user to fill at least one of them, possibly both, and I don't want the
form to validate if they're both empty.
I've tried to add the validator to one or the other, but it seems the
wrong place to put it (Cocoon forms have a strict hierarchical
structure) and in fact the behaviour with ajax and browser-update is
buggy. I've tried to add the validator to the form widget itself, but
then the error message is not picked up by the jx template, and the
user is stuck without knowing what is wrong.
Where should I put such a validator?
Tobia
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]