Welcome Guest, Not a member yet? Register   Sign In
Losing select value when using a callback

On my form I have a select menu asking for a user role. I'm using the code below to create the menu.
echo form_dropdown('role_id', $role_array, set_value('role_id'), 'id="role_id"');
I have a callback in the validation for the select menu.
$this->form_validation->set_rules('role_id', 'type of user', 'callback_role_check');
function role_check($role)
        if ($role == 0)
            $this->form_validation->set_message('role_check', $this->lang->line('user_add_role_required'));
            return FALSE;
However, when I choose a role and submit the form (incomplete), the select reverts back to the default value instead of keeping the option I selected.

If I remove the validation callback, the form works properly, keeping the option I selected.

I tried changing the code to create the menu from form_dropdown() to HTML.
<select name="role_id" id="role_id">
            <option value="0" &lt;?php echo set_select('role_id', '0', TRUE); ?&gt;>-- please select user type ---</option>
            <option value="1" &lt;?php echo set_select('role_id', '1'); ?&gt;>Admin</option>
            <option value="2" &lt;?php echo set_select('role_id', '2'); ?&gt;>Manager</option>
This made no difference.

Any ideas on what I can do to fix this?

By the way... the reason I'm using a default value of 0 (zero) is because I'm also using some jquery on the form to show another select depending on the option from this select and jquery doesn't seem to recognise an empty value in the switch statement.

Theme © iAndrew 2016 - Forum software by © MyBB