Welcome Guest, Not a member yet? Register   Sign In
form validation & filling radio input values from db query
#1

[eluser]Unknown[/eluser]
I have looked and looked and looked (forums, user guide, googled, etc.), and am not finding the answer to my problem anywhere, so figured it was time to ask for extra sets of eyes to pretty please take a look at the code before I pull my hair out. *g*

I'm trying to use the same form for both adding and editing/updating records, and the problem I'm having is getting the radio inputs to populate with their value from the database query when I load a record into the form for editing. The form works just great when adding a new record and when updating an existing record, it just does not correctly show radio values as checked when I'm initially loading the record to be edited. (Text and textarea inputs get filled in just fine, but radio and checkboxes do not get checked to match their stored values.)

I've grabbed the latest libraries/Validation.php file from svn, made one bugfix change to the same file, and created an application/libraries/MY_Validation.php with the updated regexs from here and the set_defaults() function from here.

Now, with all that said, I've tried out the form both with and without the updated validation bits, but neither version is working when I try to pre-fill the form with the values from the database. I've also tried out the code by providing the values directly, rather than grabbing from the database to make sure that's not the problem.

Help! (Code to follow.)
#2

[eluser]Unknown[/eluser]
The relevant bits of my code:

Model:
Code:
class Meta_model extends Model {

    function Meta_model() {
        parent::Model();
    }

    // _add_meta() function's here

    function _get_all_meta() {
        $this->db->select('id, mttitle')->from('meta_types')->orderby('mttitle', 'asc');
        $query = $this->db->get();
        return $query;
    }

    function _get_meta_id($edit) {
        $this->db->select('id, mttitle, mttype, mtdesc, mtswitch')->from('meta_types')->where('id', $edit)->limit(1);
        $query = $this->db->get();
        return $query;
    }

    // _update_meta() function's here
}

The controller:
Code:
class Meta extends Controller {

    function Meta() {
        parent::Controller();
        $this->load->model('Meta_model', 'Meta', TRUE);
    }

    function add() {
        // add function's here
    }

    function edit() {
        $this->load->helper('form');
        $this->load->library('validation');

        $edit = $this->uri->segment(3,0);
        if ($edit == 0) {
            $list = array();
            $query = $this->Meta->_get_all_meta();
            foreach ($query->result_array() as $row):
                $list[$row['id']] = $row['mttitle'];
            endforeach;
            $data['list'] = $list;
            $this->load->view('meta_list_all', $data);
        } else {
            $rules['id'] = "required|numeric";
            $rules['title'] = "trim|required|max_length[255]|xss_clean";
            $rules['type'] = "required";
            $rules['desc'] = "trim|xss_clean";
            $rules['trigger'] = "required";

            $fields['id'] = 'id';
            $fields['title'] = 'title';
            $fields['type'] = 'type';
            $fields['desc'] = 'description';
            $fields['trigger'] = 'trigger';

            $this->validation->set_fields($fields);

            if (empty($_POST)) {
                $query = $this->Meta->_get_meta_id($edit);
                $default = array();
                foreach ($query->result_array() as $row):
                    $default['id'] = $row['id'];
                    $default['title'] = $row['mttitle'];
                    $default['type'] = $row['mttype'];
                    $default['desc'] = $row['mtdesc'];
                    $default['trigger'] = $row['mtswitch'];
                endforeach;
                $this->validation->set_defaults($default);
            }

            $this->validation->set_error_delimiters('<br /><span class="error">', '</span>');
            $this->validation->set_rules($rules);

            if ($this->validation->run() == FALSE) {
                $this->load->view('meta_form');
            } else {
                $id = $this->input->post('id');
                $mttitle = $this->input->post('title');
                $type = $this->input->post('type');
                $desc = $this->input->post('desc');
                $trigger = $this->input->post('trigger');

                $data = array(
                    'id' => $id,
                    'mttitle' => $mttitle,
                    'mttype' => $type,
                    'mtdesc' => $desc,
                    'mtswitch' => $trigger
                );

                $this->Meta->_update_meta($data, $id);
                $this->load->view('meta_success');
            }
        }
    }
}

And the form view (meta_form):

Code:
&lt;?
$data['title'] = ' :: Admin :: Meta Categories';
$this->load->view('header.php', $data);
?&gt;

&lt;body&gt;

<h2 id="header">Meta Categories</h1>

<div id="content">

&lt;?
$url = $this->uri->segment(2) . '/' .$this->uri->segment(3);
echo form_open('meta/'.$url);

if ($this->uri->segment(2) == 'edit'): ?&gt;
&lt;input type="hidden" name="id" value="&lt;?=$this-&gt;validation->id;?&gt;" />
&lt;? endif; ?&gt;

<table cellpadding="5">

<tr>
<td><label for="title">Meta category title:</label></td>
<td>&lt;input type="text" name="title" value="&lt;?=form_prep($this-&gt;validation->title);?&gt;" size="50" max="255" /> &lt;?=$this->validation->title_error; ?&gt;</td>
</tr>

<tr>
<td><label for="type">How many subcategories may be selected at once?</label></td>
<td>&lt;input type="radio" name="type" value="0"&lt;?=$this-&gt;validation->set_radio('type', '0');?&gt; />&nbsp;Only one &lt;input type="radio" name="type" value="1"&lt;?=$this-&gt;validation->set_radio('type', '1');?&gt; />&nbsp;More than one &lt;?=$this->validation->type_error; ?&gt;</td>
</tr>

<tr>
<td><label for="desc">Description:</label></td>
<td>&lt;textarea name="desc" rows="10" cols="30"&gt;&lt;?=form_prep($this->validation->desc);?&gt;&lt;/textarea&gt;&lt;?=$this->validation->desc_error; ?&gt;</td>
</tr>

<tr>
<td><label for="trigger">Switch this meta category on or off:</label></td>
<td>&lt;input type="radio" name="trigger" value="1"&lt;?=$this-&gt;validation->set_radio('trigger', '1');?&gt; />&nbsp;On &lt;input type="radio" name="trigger" value="0"&lt;?=$this-&gt;validation->set_radio('trigger', '0');?&gt; />&nbsp;Off &lt;?=$this->validation->trigger_error; ?&gt;</td>
</tr>

<tr>
<td colspan="2">&lt;input type="submit" value="Submit" /&gt;</td>
</tr>

</table>

&lt;/form&gt;

<p>&lt;?=anchor('meta', 'Back to meta type index.');?&gt;</p>

</div>

&lt;? $this->load->view('footer.php'); ?&gt;

Thanks! Smile
#3

[eluser]chreestopher[/eluser]
I am having this issue also, my drop down boxes are not functioning either.




Theme © iAndrew 2016 - Forum software by © MyBB