CGI-FormBuilder

 view release on metacpan or  search on metacpan

lib/CGI/FormBuilder.pm  view on Meta::CPAN

    var invalid_fields = new Array();

EOJ1
    if (invalid > 0 || alertstr != '') {
EOJ2

        # Check to see if we have our own jserror callback on form failure
        # if not, then use the builtin one. Aka jsalert
        if (my $jse = $self->jserror) {
            $jsfunc .= "        return $jse(form, invalid, alertstr, invalid_fields);\n";
        } else {
            # Finally, close our JavaScript if it was opened, wrapping in <script> tags
            # We do a regex trick to turn "%s" into "+invalid+"
            (my $alertstart = $self->{messages}->js_invalid_start) =~ s/%s/'+invalid+'/g;
            (my $alertend   = $self->{messages}->js_invalid_end)   =~ s/%s/'+invalid+'/g;

            $jsfunc .= <<EOJS;
        if (! invalid) invalid = 'The following';   // catch for programmer error
        alert('$alertstart'+'\\n\\n'
                +alertstr+'\\n'+'$alertend');
        return false;
EOJS
        }

        # Close the function
        if (my $jss = $self->jsvalid) {
            $jsfunc .= "    }\n    return $jss(form);\n}\n";
        } else {
            $jsfunc .= "    }\n    return true;  // all checked ok\n}\n";
        }

        # Must set our onsubmit to call validate()
        # Unfortunately, this introduces the requirement that script()
        # must be generated/called before start() in our template engines.
        # Fortunately, that usually happens anyways. Still sucks.
        $self->{onsubmit} ||= "return $jsname(this);";
    }

    # set <script> now to the expanded javascript
    return '<script type="text/javascript">'
         . "<!-- hide from old browsers\n"
         #. "<![CDATA[\n"    # fucking web "standards"
         . $jshead . $jsmisc . $jsfunc 
         #. "\n]]>"
         . "//-->\n</script>";
}

sub script_growable {
    my $self = shift;
    return '' unless my @growable = grep { $_->growable } $self->field;

    my $jspre  = $self->jsprefix || '';
    my $jsmisc = '';

    my $grow = $self->growname;
    $jsmisc .= <<EOJS;
var ${jspre}counter = new Object;  // for assigning unique ids; keyed by field name
var ${jspre}limit   = new Object;  // for limiting the size of growable fields
function ${jspre}grow (baseID) {
    // inititalize the counter for this ID
    if (isNaN(${jspre}counter[baseID])) ${jspre}counter[baseID] = 1;

    // don't go past the growth limit for this field
    if (${jspre}counter[baseID] >= ${jspre}limit[baseID]) return;

    var base = document.getElementById(baseID + '_' + (${jspre}counter[baseID] - 1));

    // we are inserting after the last field
    insertPoint = base.nextSibling;

    // line break
    base.parentNode.insertBefore(document.createElement('br'), insertPoint);

    var dup = base.cloneNode(true);

    dup.setAttribute('id', baseID + '_' + ${jspre}counter[baseID]);
    base.parentNode.insertBefore(dup, insertPoint);

    // add some padding space between the field and the "add field" button
    base.parentNode.insertBefore(document.createTextNode(' '), insertPoint);

    ${jspre}counter[baseID]++;

    // disable the "add field" button if we are at the limit
    if (${jspre}counter[baseID] >= ${jspre}limit[baseID]) {
        var addButton = document.getElementById('$grow' + '_' + baseID);
        addButton.setAttribute('disabled', 'disabled');
    }    
}

EOJS

    # initialize growable counters
    for (@growable) {
        my $count = scalar(my @v = $_->values);
        $jsmisc .= "${jspre}counter['$_'] = $count;\n" if $count > 0;
        # assume that values of growable > 1 provide limits
        my $limit = $_->growable;
        if ($limit && $limit ne 1) {
            $jsmisc .= "${jspre}limit['$_'] = $limit;\n";
        }
    }
    return $jsmisc;
}

sub script_otherbox {
    my $self = shift;
    return '' unless my @otherable = grep { $_->other } $self->field;

    my $jspre  = $self->jsprefix || '';
    my $jsmisc = '';
    
    $jsmisc .= <<EOJS;
// turn on/off any "other"fields
function ${jspre}other_on (othername) {
    var box = document.getElementById(othername);
    box.removeAttribute('disabled');
}

function ${jspre}other_off (othername) {
    var box = document.getElementById(othername);



( run in 1.175 second using v1.01-cache-2.11-cpan-2398b32b56e )