Welcome Guest, Not a member yet? Register   Sign In
Re-populating select from db and validate it?
#1

[eluser]felyx[/eluser]
I came up with a messy solution where I validate the select with a callback function and the controller gets data from the db then passes it to the view and the view checks if there is a db value at first time like this:

Code:
<select name="subscription">
&lt;?php if ( (isset($default['subscription'])) AND (!empty($default['subscription'])) ): ?&gt;
<option value="&lt;?php echo $default['subscription']; ?&gt;" &lt;?php echo set_select('subscription', $default['subscription'], TRUE); ?&gt; >&lt;?php echo $default['subscription']; ?&gt;</option>
<option value="Choose subscription!" &lt;?php echo set_select('subscription', 'Choose subscription!'); ?&gt; >Choose subscription!</option>
&lt;?php else: ?&gt;
<option value="Choose subscription!" &lt;?php echo set_select('subscription', 'Choose subscription!', TRUE); ?&gt; >Choose subscription!</option>
&lt;?php endif; ?&gt;
.....
more options here
</select>

Well I am not sure if this is the good solution so I am curious what do you think I should have done here?

Edit: This is an editing page so at the first time I need to load data from the db to the form fields which I can then modify and save.
#2

[eluser]The Wizard[/eluser]
hello,

for selects its good to them with form helper, try a search and you will get some very nice variants.
#3

[eluser]felyx[/eluser]
[quote author="herrkaleun" date="1233521562"]hello,

for selects its good to them with form helper, try a search and you will get some very nice variants.[/quote]

Well I have no idea what you mean, I am already using set_select() but the problem cannot be solved with that function. Could you please explain what you mean?
#4

[eluser]plainas[/eluser]
Are you aware of the validation class?
http://ellislab.com/codeigniter/user-gui...ation.html
#5

[eluser]felyx[/eluser]
[quote author="plainas" date="1233539716"]Are you aware of the validation class?
http://ellislab.com/codeigniter/user-gui...ation.html[/quote]

As you can see in my code above I do. Smile
#6

[eluser]The Wizard[/eluser]
i use it like this:

Code:
function dropdown_event_status( $name, $selected_id = '' )
    {

        $this->db->select('id, status_id, name');

        $this->db->from('event_status');

        $query = $this->db->get();

        $num_rows = $query->num_rows();

        if ( $num_rows <= 0)
        {
            die ('nothing returned');
        }
        else
        {

            $result_array = array();
            foreach ($query->result_array() as $row)
            {
                $temp = array( $row['status_id'] => $row['name']  );
                $result_array = array_merge( $result_array, $temp );
            }

            $this->load->helper('form');
            return form_dropdown( $name, $result_array, $selected_id );
        }

    }

    $data['values']['status_id']        = $this->model_event->dropdown_event_status( 'status_id', 'SELECTED' ); // this gives us the selected

and in the form i use:
Code:
Status ID: &lt;?= $values['status_id'] ?&gt;


1. i generate the whole dropdown from code
2. i kinda hack use it for the validation
3. more practical, much nicer
4. http://ellislab.com/forums/viewthread/103419/
check out my tutorial
#7

[eluser]felyx[/eluser]
Thank you but I am trying to avoid generating html code with these form functions if I can.
#8

[eluser]plainas[/eluser]
[quote author="felyx" date="1233540501"][quote author="plainas" date="1233539716"]Are you aware of the validation class?
http://ellislab.com/codeigniter/user-gui...ation.html[/quote]

As you can see in my code above I do. Smile[/quote]

????
I am sorry, but nothing on the above code tells me you're using the validation class. On the contrary in fact, if you are using the validation class then the repopulating of the the form is done with attributes like $this->validation->attribute

All you need to do is create a form with whatever you need and set as validation functions whatever you need. You can create your own validation functions and pipe them into the validation class, there's nothing stopping you from accessing the database in your custom validation functions.

Maybe it's just me no understanding your question.
#9

[eluser]felyx[/eluser]
When someone loads the editing page for the first time I want to populate the form with data from the database. But at the select menu its a bit hard to do that, at the set_value function you can just set the default value parameter but you don't have that at the set_select() function, you can only tell the function if an option is the default or not.

About the validation class, as you can see I am using the set_select() function which is mentioned in the validation class reference, that's what I meant.
#10

[eluser]felyx[/eluser]
Well I am still trying to work out this problem and I would thank if someone could help me with this.

Example controller code:
Code:
function edit()
    {

        $id = $this->input->get('id', TRUE);

        $this->load->helper(array('form'));
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="warningbox" style="width: 400px;">', '</div><br />');

        $this->form_validation->set_rules('category', 'Category', 'callback_select_check');
        $this->form_validation->set_rules('subcategory', 'Subcategory', 'callback_select_check');

        $this->db->select('cat_name, subcat_names');
        $cat_query = $this->db->get('categories');

        if ($cat_query->num_rows() > 0)
        {
            foreach ($cat_query->result_array() as $row)
            {
                $categories[] = $row['cat_name'];
                $subcategories[$row['cat_name']] = $row['subcat_names'];
            }
        }

        sort($categories);
        $data['categories'] = $categories;
        $data['subcategories'] = $subcategories;

        $this->db->where('id', $id);
        $query = $this->db->get('datatable');
        $default = $query->row_array();
        $data['default'] = $default;

        $data['id'] = $id;

        if ( ($this->input->post('cancel') == FALSE) AND ($this->form_validation->run() == FALSE) )
            {

                //load the view with $data

            } elseif ( $this->input->post('cancel') == TRUE )
            {
                //redirect to the listing page
            }
            else
            {

                //update data in db and redirect to listing page

            }

    }

function select_check($str)
    {
        if ($str == 'Please Choose!')
        {
            $this->form_validation->set_message('select_check', 'Please Choose!');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

Example view code:

Code:
&lt;form method="POST" action="?c=adm_oneyearad&m=edit&id;=&lt;?php echo $id; ?&gt;"&gt;
            <label>Category:</label>
            <p><select name="category" class="combomid">
                <option value="Please Choose!" &lt;?php echo set_select('category', 'Please Choose!', TRUE); ?&gt; >Please Choose!</option>
                &lt;?php if ( (isset($default['category'])) AND (!empty($default['category'])) ): ?&gt;
                    &lt;?php
                        foreach($categories as $category)
                        {
                            if ($category == $default['category'])
                            {
                                echo '<option value="' . $category . '" ' . set_select('category', $category) . ' selected="selected" >' . $category . '</option>';
                            } else {
                                echo '<option value="' . $category . '" ' . set_select('category', $category) . ' >' . $category . '</option>';
                            }
                        }
                    ?&gt;
                &lt;?php else: ?&gt;
                    &lt;?php foreach($categories as $category): ?&gt;
                        <option value="&lt;?php echo $category ?&gt;" &lt;?php echo set_select('category', $category); ?&gt; >&lt;?php echo $category ?&gt;</option>
                    &lt;?php endforeach; ?&gt;
                &lt;?php endif; ?&gt;
            </select></p>
            &lt;?php echo form_error('category'); ?&gt;
            <label>Subcategory:</label>
            <p><select name="subcategory" class="combomid">
                <option value="Please Choose!" &lt;?php echo set_select('subcategory', 'Please Choose!', TRUE); ?&gt; >Please Choose!</option>

               &lt;?php if ( (isset($_POST['category'])) AND ($_POST['category'] != 'Please Choose') ): ?&gt;
                    &lt;?php
                        $get_subcats = $subcategories[$_POST['category']];
                        $list_subcats = explode(",", $get_subcats);
                        sort($list_subcats);
                        foreach($list_subcats as $subcategory_item) {
                            echo '<option value="'.$subcategory_item.'"' . set_select('subcategory', $subcategory_item) . '>'.$subcategory_item.'</option>';
                        }
                    ?&gt;
                &lt;?php elseif ( (isset($default['category'])) AND (!empty($default['category'])) ): ?&gt;
                    &lt;?php
                        $get_subcats = $subcategories[$default['category']];
                        $list_subcats = explode(",", $get_subcats);
                        sort($list_subcats);
                        foreach($list_subcats as $subcategory_item) {
                            if ($subcategory_item == $default['subcategory'])
                            {
                                echo '<option value="'.$subcategory_item.'"' . set_select('subcategory', $subcategory_item) . ' selected="selected">'.$subcategory_item.'</option>';
                            } else {
                                echo '<option value="'.$subcategory_item.'"' . set_select('subcategory', $subcategory_item) . '>'.$subcategory_item.'</option>';
                            }


                        }
                    ?&gt;
                &lt;?php endif; ?&gt;


            </select></p>
            &lt;?php echo form_error('subcategory'); ?&gt;
&lt;/form&gt;

I will post the javascript which populates the subcategory dropdown. The basic idea is that when you select a category in the category dropdown the javascript populates the data in the subcategory dropdown according to that. So when you go to the edit page (the first time) it should show the data in the form from the database which you could modify and it works for the most inputs except the select.




Theme © iAndrew 2016 - Forum software by © MyBB