Welcome Guest, Not a member yet? Register   Sign In
set_checkbox (partially resolved)
#1

[eluser]ivston[/eluser]
Any idea why the following code does not set the checkbox properly ?

form-validation rule:
Code:
array('field'=>"cat1[]",     'label'=>"Cur",        'rules'=>''),

form:
Code:
for (... here I set $val ...)
echo form_checkbox(array('name'=>"cat1[]",'value'=>$val,'checked'=>set_checkbox("cat1[]",$val,TRUE)))

- the form-validation library is properly loaded.
- it sets properly as checked the fields, but keep them always checked after the interaction.

Thanks!
#2

[eluser]bigtony[/eluser]
set_checkbox() is intended for use when you are not using the form_checkbox() function:
Code:
<input type="checkbox" name="cat1[]" value="<?php echo $val; ?>" <?php echo set_checkbox('cat1[]', $val); ?> />
When using form_checkbox, code it like this:
Code:
// assumes $val can only be 0 or 1...
<?php echo form_checkbox("cat1[]", $val, ($val == 1)); ?>
#3

[eluser]ivston[/eluser]
I also had tried, this, similar to to your post, but it still does not work..
Code:
echo "<input type='checkbox' name='cat1[]' value='".$val."' ".set_checkbox("cat1[]",$val,TRUE)";
#4

[eluser]ivston[/eluser]
pp. this is done with CI 1.7.1
#5

[eluser]bigtony[/eluser]
[quote author="ivston" date="1258495038"]I also had tried, this, similar to to your post, but it still does not work..
Code:
echo "<input type='checkbox' name='cat1[]' value='".$val."' ".set_checkbox("cat1[]",$val,TRUE)";
[/quote]
It's not similar enough to my post. For a start, you are still passing 3 parameters to set_checkbox() when it only accepts 2, and you're not ending the input tag.
#6

[eluser]ivston[/eluser]
- the missing closure of the input tag is just a tipo in the post; in the code it's ok. All the rest is the same, except the default value which I do need to set.

- as for the "only" two input params, how about this declaration in /system/libraries/Form_validation.php:

Code:
function set_checkbox($field = '', $value = '', $default = FALSE)

Moreover, how about this info in the CodeIgniter documentation (http://ellislab.com/codeigniter/user-gui...ation.html) of set_checkbox():

"Permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE)."

Any valuable idea about my problem ? I have no problem using this class in other, more simple cases.

pp. I just noticed that I autoload "validation". However, the class that actually permits a default value of set_checkbox() is FormValidation. I don't know how this could explain the story, since I did some control print_r()'s in FormValidation.php; some of which did print something, so this library seems to have been loaded. (now I can not check whether autoloading "formvalidation" is possible and whether this would resolve my problem).
#7

[eluser]ivston[/eluser]
:-) I partially resolved the problem: in this case one must autoload or load 'form_validation' instead of 'validation'; the former does- the later is obsolete and does not- supports defaults for set_checkbox

there is still one problem: autoloading rules corresponding to my controller-class / function does not work here (for other functions it does work). I do it with this code and I don't see any problem with it:

Code:
$config=array(
... other declarations ...
'mycontroller/myfunction' => array(
    array('field'=>'cat1[]',     'label'=>'myLabel1',        'rules'=>'xss_clean'),
    array('field'=>'cat2[]',     'label'=>'myLabel2',        'rules'=>'xss_clean')
    ),
... other declarations ...
);

I resolved the missing autoload by explicitly setting the rules in my controller, such as:
Code:
$this->form_validation->set_rules('cat1[]',"myLabel1",'xss_clean')
$this->form_validation->set_rules('cat1[]',"myLabel2",'xss_clean')

So, admins, please, tell me what is wrong with my rules autoloading? Then, please, update the autoloading example in config/autoload.php so newbies don't loose time for such naive problems.

cheers
#8

[eluser]Samuurai[/eluser]
Is there a better solution than this? I just spent a couple of hours setting up the form_validation config file.. surely set_checkbox can be made to work with my configuration too..?
#9

[eluser]ivston[/eluser]
my final solution was not using form validation at all; to eventually speedup the code. Here is an abstraction of my solution:

in controller initialization:
Code:
session_start();
if (!isset($_SESSION['cat']))
   $_SESSION['cat'] = array(2,4,8);    // set defaults of 3 check-boxes (done here because used in various response functions)

in a controller function that responds to the post:
Code:
$Cat=$this->input->post('cat');
if (!empty($Cat))
   $_SESSION['cat']=$Cat;              // update the checkboxes state with posted values;

in a viewer function that prepares the code:
Code:
$ind=1;
$CatList = array('cat1','cat2','cat3');
foreach ($CatList as $catItem){
   $catVal=pow(2,$ind++);    // different values for each checkbox from the set
   $chk=(empty($_SESSION['cat'])||in_array($val,$_SESSION['cat'])) ? "checked='checked'" : "";
   echo "<input type='checkbox' name='cat[]' value='".$catVal."' ".$chk." />".$catItem." ";
}

for live demo, see Artjsta - the artists site It's in italian, but the checkbox functionality is clear




Theme © iAndrew 2016 - Forum software by © MyBB