1
jlm69
formselect.php validation not working - ALL Xoops versions
  • 2010/6/6 10:55

  • jlm69

  • Module Developer

  • Posts: 719

  • Since: 2002/7/19


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.

Maybe this is not the best way to change this, but it works.

Thanks,

John

2
Peekay
Re: formselect.php validation not working - ALL Xoops versions
  • 2010/6/6 13:36

  • Peekay

  • XOOPS is my life!

  • Posts: 2335

  • Since: 2004/11/20


Hi John,

I too reported this problem in this post.

In my experience it affects DHTML text area too?.

I simply commented-out the JS code and everything works, so I am wondering that by adding code to every class it conflicts with the validation code in the main xoops form class.

If this is a bug, I would welcome a confirmation and an official 'fix' so I can alert module users that they need to change those Xoops core files.
A thread is for life. Not just for Christmas.

3
jlm69
Re: formselect.php validation not working - ALL Xoops versions
  • 2010/6/6 14:49

  • jlm69

  • Module Developer

  • Posts: 719

  • Since: 2002/7/19


Peekay,

How are you.

I have not had the problem for dhtml text area, just the select boxes in my modules.

But if commenting them out works, then that means it is done somewhere else, probably formelement.php. No need to do it twice, is there?

But we do need an official fix, either by removing it from some files or fixing it like I did for formselect.php.

Thanks,

John

4
Peekay
Re: formselect.php validation not working - ALL Xoops versions
  • 2010/6/10 11:32

  • Peekay

  • XOOPS is my life!

  • Posts: 2335

  • Since: 2004/11/20


Bump. Can anyone from the core team take a look at this?
A thread is for life. Not just for Christmas.

5
jlm69
Re: formselect.php validation not working - ALL Xoops versions
  • 2010/6/21 11:01

  • jlm69

  • Module Developer

  • Posts: 719

  • Since: 2002/7/19


I will bump this back out. I hope someone will point this out to the core team.

Thank You,

John

6
Mamba
Re: formselect.php validation not working - ALL Xoops versions
  • 2010/6/21 15:03

  • Mamba

  • Moderator

  • Posts: 11409

  • Since: 2004/4/23


Reported in tracker

The best way is always to report it in Source Forge Tracker, with link to the Forum discussion
Support XOOPS => DONATE
Use 2.5.11 | Docs | Modules | Bugs

7
trabis
Re: formselect.php validation not working - ALL Xoops versions
  • 2010/9/18 13:46

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


This code works:
$checkbox2 = new XoopsFormSelect('Select Caption2''select2'null3); $checkbox2->addOption(0'Yes'); $checkbox2->addOption(1'No'); $checkbox2->addOption(2'Maybe'); $form->addElement($checkbox2true);


I assume that you are using select box with 1 field height, in that case the current field is marked as selected(by the browser I guess) and will always pass validation.

I assume you are using something like ''=> 'Select a field';
Notice that your solution will also fail validation if the first option is not empty. Example:('field_0'=>'Yes', 'field_1'=>'No')

I would prefer not changing core. You could change the size of the select box, just make it > 1;

Regarding the DhtmlTextArea, it should be fixed in 2.5 now.

I've created a testform.php in the root of my Xoops Instalation. Here is the code I used:
include 'mainfile.php';
include 
'header.php';
xoops_load('xoopsformloader');
$form = new XoopsThemeForm('Form''form''testform.php');
$checkbox = new XoopsFormCheckBox('Check Caption''check');
$checkbox->addOption(0'Yes');
$checkbox->addOption(1'No');
$checkbox->addOption(2'Maybe');
$form->addElement($checkboxtrue);

$checkbox2 = new XoopsFormSelect('Select Caption2''select2'null3);
$checkbox2->addOption(0'Yes');
$checkbox2->addOption(1'No');
$checkbox2->addOption(2'Maybe');
$form->addElement($checkbox2true);

$form->addElement(new XoopsFormDhtmlTextArea('Text'"text"), true);

$editor = new XoopsFormEditor('Text2''text2', array('editor' => 'DhtmlTextarea'));
$form->addElement($editortrue);

$editor2 = new XoopsFormEditor('Text3''DhtmlTextarea', array('name' => 'text3'));
$form->addElement($editor2true);

$button = new XoopsFormButton('Submit''submit''Submit button''submit');
$form->addElement($button);

$form->display();

include 
'footer.php';
?>

8
satrebil
Re: formselect.php validation not working - ALL Xoops versions
  • 2011/3/1 14:56

  • satrebil

  • Just popping in

  • Posts: 15

  • Since: 2009/5/3 2


Hello,

To solve the problem in checkbox validation with a single option, you must modify the file formcheckbox.php located in the / root / class / xoopsform directory.

We must replace the line 241 (in the XOOPS version 2.5.0) that contains:
return NWLINE "var hasChecked = false; var checkBox = myform.elements['{$eltname}'];" "for ( var i = 0; i < checkBox.length; i++ ) { if (checkBox[i].checked == true) { hasChecked = true; break; } }" "if (!hasChecked) { window.alert("{$eltmsg}"); checkBox[0].focus(); return false; }";

by
return NWLINE "var hasChecked = false; var checkBox = myform.elements['{$eltname}']; if (checkBox.length) {for (var i = 0; i < checkBox.length; i++) {if (checkBox[i].checked == true) {hasChecked = true; break;}}}else{if (checkBox.checked == true) {hasChecked = true;}}if (!hasChecked) {window.alert("{$eltmsg}");if (checkBox.length) {checkBox[0].focus();}else{checkBox.focus();}return false;}";


This is because if there is only one option the element is not an array.
We must first check if the length is not null to determinate if there is only one or more options in the checkbox.

Satrebil
(Excuse me for my english)


9
jlm69
Re: formselect.php validation not working - ALL Xoops versions
  • 2011/3/2 12:42

  • jlm69

  • Module Developer

  • Posts: 719

  • Since: 2002/7/19


Please read whole post.

This post was originally about select boxes not checkboxes. But they both seem to have this problem.

First I never said my fix was the right fix, it just works for my modules.

Ghia wrote

Quote:
I would prefer not changing core. You could change the size of the select box, just make it > 1;


First, Why would you not want to fix the core? Shouldn't it work under any circumstance?

Second, No I can't change the size of the select box.

It would be better if the core was changed to fit all uses. I would prefer that I and users of my module not have to change the core, the core team should change the core.



Peekay says that if you remove that code all together it works correctly. How can that be? There must be double code. MAYBE that is where the core team should look. But this has never worked in any version of Xoops, so I think it is time to fix this.



Satrebil has found the same problem with checkboxes, Please fix the core!



Thanks,

John

Login

Who's Online

287 user(s) are online (245 user(s) are browsing Support Forums)


Members: 0


Guests: 287


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits