[Zope] dtml syntax question

Evan Simpson evan@4-am.com
Tue, 09 Nov 1999 02:05:09 +0000


Sam Gendler wrote:

> So what do I do, <dtml-var form['oname:required'].value> would seem the
> logical choice for syntax.  Correct?

Perhaps I'm not understanding, but I thought you wanted to do this in
J[ava]Script? Here's some code which uses the form.elements array for mass
validation rather than individual form['foo:tag'] accesses:

// This is a set of generic JavaScript routines designed to validate form
// input. Validation is performed for each text field based on the name
// of the field including a "tag". Inspired by the automatic type-conversion
// done by Bobo (http://www.digicool.com/releases/bobo)
//
// Author: Andrew Snare (ajs@pigpond.com)
//   Date: 5th September, 1998

// Each function in this group is called as required to verify fields of
// a specific type.

function text2float(element) {
    // Test for anything unparsable as a number.
    if (isNaN(parseFloat(element.value))) {
        alert("You have not entered a required number.");
        element.focus();
        return false;
    }
    return true;
}

function text2int(element) {
    // Test for anything unparsable as a number, or that has decimal places.
    if (isNaN(parseInt(element.value)) ||
        (parseInt(element.value) != parseFloat(element.value))) {
        alert("You have not entered a required whole number (without fractions
or decimals).");
        element.focus();
        return false;
    }
    return true;
}
text2long = text2int;

function text2string(element) {
    // Anything can be represented as a string.
    return true;
}

function text2required(element) {
    // Just test for non-empty string.
    if (element.value + "" == "") {
        alert("You have not specified some required information.");
        element.focus();
        return false;
    }
    return true;
}

// This is a list of all the types we know how to verify. These correspond
// to the functions above.

conversionList = new Array();
conversionList[0] = "float";
conversionList[1] = "int";
conversionList[2] = "long";
conversionList[3] = "string";
conversionList[4] = "required";

// Call this function to validate as many of the text fields in the given
// form as possible. Suitable for use as:
//
//     <FORM .... onSubmit="return Validate(this);">

function Validate(form) {
    for (var i=0; i<form.elements.length; i++) {
        with (form.elements[i])
            {
                if (type == "text") {
                    var lio = name.lastIndexOf(":");
                    if (lio != -1 && lio < (name.length-1)) {
                        var conversion = name.substring(lio+1,name.length);
                        var canconvert = false;
                        var j = 0;
                        while (!canconvert && j<conversionList.length) {
                            if (conversionList[j] == conversion) {
                                canconvert = true;
                                break;
                            }
                            j++;
                        }
                        if (!canconvert) {
                            break;
                        }
                        if (!eval("text2"+conversion+"(form.elements[i])"))
                            return false;
                    }
                }
            }
    }
    return true;
}

Cheers,

Evan @ 4-am