Welcome Guest, Not a member yet? Register   Sign In
set_select() issue
#1

[eluser]ebohling[/eluser]
i'm sure i'm pulling a newbie mistake, but even after scouring the forums, wikis, user guide and google i cannot find my issue (probably bonehead move #2)...

i'm simply trying to select the user's drop down selection after a validation error occurs. my HTML (view) code is simple enough (i'm showing two methods, please feel free to explain which one is better an why...remember i'm a newbie):

<label for="opportunity-name-select">Opportunity:</label><br/>
&lt;?=form_dropdown('opportunity-name-select', $ops, '', 'id="opportunity-name-select"');?&gt;
&lt;?=form_error('opportunity-name-select'); ?&gt;

<label for="scenario-name-select">Scenario:</label><br/>
<select name="scenario-name-select" id="scenario-name-select">
<optgroup label="scenarios">
<option value="0">-- I want to add my own Scenario. --</option>
&lt;?php foreach($scen as $row):?&gt;
<option value="&lt;?=$row->scenario_id; ?&gt;" &lt;?=set_select('scenario-name-select', $row->scenario_id); ?&gt;>&lt;?=$row->scenario_name; ?&gt;</option>
&lt;?php endforeach;?&gt;
</optgroup>
</select>

the database (model) code (again two methodologies used, best practices feedback welcome):

function getOpportunities()
{
$query = $this->db->query('select opportunity_name, opportunity_id from Opportunities order by opportunity_name');
if ($query->num_rows() < 0) {
show_error('Database is empty!');
} else {
$q = $query->result_array();
foreach($q as $cRow) {
$data['opportunities'][$cRow['opportunity_id']] = ($cRow['opportunity_name']);
}
return $data;
}
}

function getScenarios()
{
$query = $this->db->query('select scenario_name, scenario_id from Scenarios order by scenario_name');
if ($query->num_rows() < 0) {
show_error('Database is empty!');
} else {
// $q = $query->result_array();
// foreach($q as $cRow) {
// $data['scenarios'][$cRow['scenario_id']] = ($cRow['scenario_name']);
// }
// return $data;
return $query->result();
}
}

what am i doing wrong? upon a validation error, the drop down list just defaults back to first option. let me know if you need to see more code to answer.

thanks,
--bb--
#2

[eluser]ebohling[/eluser]
any ideas...anyone? anyone?
#3

[eluser]richwalkup[/eluser]
I'm quite sure this is a bug - I have worked around it myself over the past couple days and considered entering a bug report on it as well. You can find a workaround here (but it requires modifying the core):

http://ellislab.com/forums/viewreply/593430/

#EDIT: I have found this to have other buggy artifacts - don't use it. I don't have time to figure out what it is right now but there is an issue here that should be easily worked around without adding blank rules for every field.
#4

[eluser]seanloving[/eluser]
I just posted a question as a reply to another thread, but I'm not sure if it will get the attention of the larger audience. I'm not trying to post twice, but here's my question...

http://ellislab.com/forums/viewthread/11...80/#594552

it appears I might be facing similar issue. Anyone?

--Sean Loving
#5

[eluser]richwalkup[/eluser]
I'm going to reply in this forum because I think the issue is very likely related to this bug. The fast way to find out is to add an empty validation requirement to your code. If something like the code below does not fix your issue, then it's more likely related to the usage and/or scope of the variable at the time you are attempting to access it and then I would have to see more of the code to understand the full scope of its usage.

Code:
$this->form_validation->set_rules('group_id', 'Group', '');
#6

[eluser]seanloving[/eluser]
I tried your suggestion on line 6 but my line 36 still seems to cause same error.

Note: this function comes with Auth Library by Adam Griffith. I want a simple modification to display the three possible values of the group_id field (users table) in a pull-down select list:

admin
sales
customer

The default value to display on the item list should be the value of $userdata->group_id.



Code:
function register($login = TRUE, $edit = FALSE, $id = NULL)
    {
        if($edit === TRUE)
        {
            $this->CI->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
            $this->CI->form_validation->set_rules('group_id', 'Group', '');
        }
        else
        {
            $this->CI->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[40]|callback_reg_username_check');
            $this->CI->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[12]|matches[conf_password]');
            $this->CI->form_validation->set_rules('conf_password', 'Password confirmation', 'trim|required|min_length[4]|max_length[12]|matches[password]');
            $this->CI->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_reg_email_check');
        }
        
        if($this->CI->form_validation->run() == FALSE)
        {
            if($edit === TRUE)
            {
                $query = $this->CI->db->query("SELECT * FROM `users` WHERE `id` = '$id'");
                $result = $query->result_array();
                
                $this->view('register', $result[0]);
            }
            else
            {
                $this->view('register');
            }
        }
        else
        {
            // the form was valid and scrubbed
            $username = set_value('username');
            $password = $this->_salt(set_value('password'));
            $email = set_value('email');
            $group = set_select('group_id', '$this->CI->input->post(\'group_id\')');
            //$group = $this->CI->input->post('group_id');
            
            if($edit === TRUE)
            {
                $this->CI->db->query("UPDATE `users` SET `email` = '$email' WHERE `id` = '$id'");
                $this->CI->db->query("UPDATE `users` SET `group_id` = '$group' WHERE `id` = '$id'");
                $data2['msg'] = "The user has now been edited.";
            }
            else
            {
                $this->CI->db->query("INSERT INTO `users` (username, email, password) VALUES ('$username', '$email', '$password')");
                $data2['msg'] = "The user has now been created.";
            }
            
            if($login === TRUE)
            {
                $data2['msg'] = "The user has been created, you have now been logged in.";
                
                $userdata = $this->CI->db->query("SELECT * FROM `users` WHERE `username` = '$username'");
                $row = $userdata->row_array();
            
                $data = array(
                        'username' => $username,
                        'user_id' => $row['id'],
                        'group' => $row['group_id'],
                        'logged_in' => TRUE
                        );
                $this->CI->session->set_userdata($data);
            
                if($this->config['auth_remember'] === TRUE)
                {
                    $this->_generate();
                }
            }
            
            $this->view('reg_success', $data2);
        }
    } // function register()
And here is the view file called register.php
Code:
<div id="login">
    
    &lt;?php if(empty($username)) { ?&gt;
    <h2>Register</h2>
    &lt;?php } else { ?&gt;
    <h2>Update</h2>
    &lt;?php } ?&gt;
    
    <div class="box">
            &lt;form method="post"&gt;
            &lt;?php if(empty($username)) { ?&gt;
            Username:<br />
            &lt;input type="text" name="username" size="50" class="form" value="&lt;?php echo set_value('username'); ?&gt;" /&gt;&lt;br />&lt;?php echo form_error('username'); ?&gt;<br />
            Password:<br />
            &lt;input type="password" name="password" size="50" class="form" value="&lt;?php echo set_value('password'); ?&gt;" /&gt;&lt;?php echo form_error('password'); ?&gt;<br /><br />
            Password confirmation:<br />
            &lt;input type="password" name="conf_password" size="50" class="form" value="&lt;?php echo set_value('conf_password'); ?&gt;" /&gt;&lt;?php echo form_error('conf_password'); ?&gt;<br /><br />
            &lt;?php } ?&gt;
            Email:<br />
            &lt;?php if(empty($username)){ ?&gt;
                &lt;input type="text" name="email" size="50" class="form" value="&lt;?php echo set_value('email'); ?&gt;" /&gt;&lt;?php echo form_error('email'); ?&gt;<br /><br />
            &lt;?php }else{ ?&gt;
            &lt;input type="text" name="email" size="50" class="form" value="&lt;?php echo set_value('email', $email); ?&gt;" /&gt;&lt;?php echo form_error('email'); ?&gt;<br /><br />
            Group:<br />
            
            <select name="group_id">
                <option &lt;?php echo set_select('group_id', '1');?&gt; value="1">Admin</option>
                <option &lt;?php echo set_select('group_id', '2');?&gt; value="2">Sales</option>
                <option &lt;?php echo set_select('group_id', '100');?&gt; value="100">Customer</option>
            </select>
            
            &lt;?php } if(empty($username)) { ?&gt;
            &lt;input type="submit" value="Register" name="register" /&gt;
            &lt;?php } else { ?&gt;
            &lt;input type="submit" value="Update" name="register" /&gt;
            &lt;?php } ?&gt;
            &lt;/form&gt;
    </div>
</div>




Theme © iAndrew 2016 - Forum software by © MyBB