Menu

#1072 formselect.php validation not working

XOOPS_2.4.x
closed
trabis
5
2012-09-25
2010-06-21
No

See: http://www.xoops.org/modules/newbb/viewtopic.php?post_id=328977#forumpost328977

If you have a form with a select box and you set it to true for validation it does not work. I found this with all Xoops versions. My modules need it to work right so here is what I found.

It is in the function renderValidationJS

here is the function from version 2.4.4

function renderValidationJS()
{
// render custom validation code if any
if (! empty($this->customValidationCode)) {
return implode("\n", $this->customValidationCode);
// generate validation code if required
} elseif ($this->isRequired()) {
$eltname = $this->getName();
$eltcaption = $this->getCaption();
$eltmsg = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
$eltmsg = str_replace('"', '"', stripslashes($eltmsg));
return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" . "for (i = 0; i < selectBox.options.length; i++ ) { if (selectBox.options[i].selected == true) { hasSelected = true; break; } }" . "if (!hasSelected) { window.alert("{$eltmsg}"); selectBox.focus(); return false; }";
}
return '';
}

I changed it to:

function renderValidationJS()
{
// render custom validation code if any
if (! empty($this->customValidationCode)) {
return implode("\n", $this->customValidationCode);
// generate validation code if required
} elseif ($this->isRequired()) {
$eltname = $this->getName();
$eltcaption = $this->getCaption();
$eltmsg = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
$eltmsg = str_replace('"', '"', stripslashes($eltmsg));
return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" . "for (i = 0; i < selectBox.options.length; i++ ) { if (selectBox.value != "") { hasSelected = true; break; } }" . "if (!hasSelected) { window.alert("{$eltmsg}"); selectBox.focus(); return false; }";
}
return '';
}

Now it will work correctly.

I have included this file (for each Xoops Version) with the changes, in my Jobs and Classified modules.

Discussion

  • trabis

    trabis - 2010-09-18

    Select box validates if option is checked. Seems that browser checks first option if size of select box is one. I don't think it is a bug, if you want to force user to click on a option you should change select box size to a number higher then 1.
    Your propose is not valid because I may not have a empty option in first value and therefore, it will still validate when size == 1.

     
  • SourceForge Robot

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 7 days (the time period specified by
    the administrator of this Tracker).

     

Log in to post a comment.

MongoDB Logo MongoDB