CodeIgniter Forums

Full Version: Validation set_radio Function for Editing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]axpen[/eluser]
I just had an interesting problem with page editing. I was trying to use the validation field repopulate methodology to re pick my radio button. Come to find out $this->Validation->set_radio and set_checkbox assert that the validation variable be a POST variable as well. This is what it's for, for repopulating fields. However it is also inevitably going to be used for edit, as it's the best way.

I was looking for the best way to do this, so I checked bambooInvoice for an example.

Code:
<input style="width: auto;" class="requiredfield" name="save_invoices" type="checkbox" id="save_invoices" size="20" value="y" <?php
if ($this->validation->set_checkbox('save_invoices', 'y'))
{
    echo $this->validation->set_checkbox('save_invoices', 'y');
}
elseif ($this->settings_model->get_setting('save_invoices') == 'y')
{
    echo 'checked="checked"';
}
?> />

I do my code a little different. I pulled the relevant data from the database before loading the view. So loading a record again would be a waste when the validation already has the data. It is just that set_radio's explicit looking in POST is keeping it from working.

I was thinking the set_radio and checkbox could go from;

Code:
function set_radio($field = '', $value = '')
{
    if ($field == '' OR $value == '' OR  ! isset($_POST[$field]))
    {
        return '';
    }
        
    if ($_POST[$field] == $value)
    {
        return ' checked="checked"';
    }
}

to;

Code:
function set_radio($field = '', $value = '', $variable = '')
{
    if ($field == '' OR $value == '')
    {
        return '';
    }

    if (isset($_POST[$field])) { # assume field from POST
        if ($_POST[$field] == $value)
            return ' checked="checked"';
    } elseif ($variable != '') { # assume variable
        if ($variable == $value)
            return ' checked="checked"';
    }
}

I am still quite new to CodeIgniter, so please correct me if I am on the wrong track. Or point me to how this should be done or why my idea wouldn't work.

Thanks,
--Alex