Welcome Guest, Not a member yet? Register   Sign In
Form Helper: Hidden Input and Validation: Checkbox Arrays
#1

[eluser]aBoy[/eluser]
Hi. I've been using CI for about three months now. It is excellent, thank you very much. Big Grin

I got some issues so I searched the forum for a while, but still have a couple of questions about the Form Helper class.

1. Is there a way to assign an 'id' to an input_hidden function call?

2. I have a dynamicly generated array of checkboxes which allows the user to select what category their profile/article will exist in. We are only really concerned with the last 6 lines or so;

Code:
foreach ($sections as $s) {
    $checked = FALSE;
    if ($start != $s->sectionName) {
        echo "<h4>".$s->sectionName.'</h4>';
        echo "\n";
    }
    // Categories
    $checked = FALSE;
    // Compare users data vs all data
    if (isset($catID)) {
        foreach ($catID as $c) {
            if ($c->catID == $s->catID) {
                $checked = TRUE;
            }
        }
    }
    echo '<div class="indent1">';
    echo form_checkbox(
            'catID[]',
            $s->sectionID.':'.$s->catID,
            $checked,
            $this->validation->set_checkbox('catID[]', $s->sectionID.':'.$s->catID)
            );
    echo $s->catName.'</div>';
    echo "\n";
    $start = $s->sectionName;
}

The line;

Code:
echo form_checkbox(
            'catID[]',
            $s->sectionID.':'.$s->catID,
            $checked,
            $this->validation->set_checkbox('catID[]', $s->sectionID.':'.$s->catID)
            );


containing the piece of code;

Code:
$this->validation->set_checkbox('catID[]', $s->sectionID.':'.$s->catID)

does not work for an array of checkboxes using the same name. Sad The only time this is an issue is if the user creates a 'new' profile/article and misses one of the other required fields. On return to the form all the fields are re-populated except for the checkboxes.

Any ideas?
#2

[eluser]xwero[/eluser]
The form_hidden has no extra parameter i think this has to do with the possibility to add hidden inputs with the form_open tag but it can be fixed.
Code:
function form_hidden($name, $value = '', $extra = '')
{
    if ( ! is_array($name))
    {
        return '&lt;input type="hidden" name="'.$name.'" value="'.form_prep($value).'" '.$extra.' /&gt;';
    }

    $form = '';
    foreach ($name as $name => $attr)
    {
        // second ternary conditional added to be compatible with the current function behavior
            $value = (isset($attr['value']))?form_prep($attr['value']):(is_string($attr)?$attr:'');
            $extra = (isset($attr['extra']))?$attr['extra']:'';
            $form .= '&lt;input type="hidden" name="'.$name.'" value="'.$value.'" '.$extra.' /&gt;';
    }
    
    return $form;
}
The usages for adding more than one input with one form_hidden function is
Code:
$hiddens = array('name1'=>array('value'=>'value1','extra'=>'id="1"'),
                 'name2'=>array('value'=>'value2','extra'=>'id="2"'));
form_hidden($hiddens);
// or
form_open('method/controller','',$hiddens);
#3

[eluser]xwero[/eluser]
I wasn't fully awake Smile For you second question you could use MY_form_helper. It are replacement functions for the CI provided functions that are aware that the form is posted.
#4

[eluser]aBoy[/eluser]
Thanks xwero.
#5

[eluser]gkchicago[/eluser]
I was faced with the same problem as your second question regarding checkboxes and didn't like extending the form and validation libraries just to fix it. I have posted my solution to this problem here:

http://ellislab.com/forums/viewthread/79792/
#6

[eluser]vadivelan[/eluser]
Hi
CI not dealing with user form submitted arrays properly. To deal with that have a look the below thread:

http://ellislab.com/forums/viewthread/73012/




Theme © iAndrew 2016 - Forum software by © MyBB