Login Retreive lost passwordRegister
Search

Forum Index / Feedback / Checkbox validation bug?

[ This topic is solved ]

  Marko B 14 July 10 / 13:46  
I think there is something wrong with checkbox validation when I give array as $value. When I set my own custom validator function, class.Field.php returns this:

Warning (2): class.Field.php at 240 trim() expects parameter 1 to be string, array given

The problem is trim() is set around $this->getValues(), which won't work in this case, since value is array... there should be:

$value = $this->getValues();
$value = (is_array ($value)) ? $value : trim ($value);

in front of call_user_func() ....shouldn't it?

  Marko B 14 July 10 / 13:49  
Also... if more than one checkbox is checked, they are not checked when you hit "back" on form. Weird.

  Johan Wiegel (Admin) 15 July 10 / 11:00  
Can you show some code where it goes wrong?
When I test this it's working as it should.

what version of FH are you using?

  Marko B 15 July 10 / 11:35  
Changed at 15 July 10 / 11:44
I'm using latest version.
Here is the code, but I can't paste it all, too much code in classes.. this is basicaly it:


$current_page = $this->pages->getCurrentPage();
$form = new FormHandler('newad', $current_page['url']);
$form->setFocus (false);
$form->useTable (false);

$farray = {
    1 => "Opcija A",
    2 => "Opcija B",
    3 => "Opcija C"
}
$form->checkBox ("some_title", "cf3", $farray, array ($this, 'checkBoxReqValidator'));
$form->submitButton ("Next");

....

public function checkBoxReqValidator ($items)
{
    if (!empty ($items) && is_array ($items)) {
        if (sizeof ($items) >= 1) {
            return true;
        }
    }
    return "You have to select at least one option";
}


This alone gives me error I pasted before in class.Field.php, so I replaced it with (line 239):


$value = $this->getValue();
$value = (is_array ($value)) ? $value : trim ($value);
$error = call_user_func(array(&$this->_sValidator[0], $this->_sValidator[1]), $value );


which makes validator work, but when I click BACK no checkbox is checked... unless I only check ONE, when I check more, none is checked when hitting back.

  Marko B 15 July 10 / 11:40  
Changed at 15 July 10 / 11:54
Okay, I'm an idiot :) I know why it doesn't check them all on BACK... so sorry.

But the problem about validators still exists unless I remove the trim() around the value. My fix in class.Field.php works for me. Do check this part about validating checkbox, when I add array as options.

  Johan Wiegel (Admin) 15 July 10 / 12:23  
fixed that in current download

  Top


powered by PHP-GLOBE   © 2004 - 2012 FormHandler. All rights reserved.   -   Open source license