Part of the EllisLab Network
   
 
form validation & filling radio input values from db query
Posted: 07 December 2007 02:34 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  2
Joined  11-28-2007

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.)

Profile
 
 
Posted: 07 December 2007 02:35 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Avatar
Total Posts:  2
Joined  11-28-2007

The relevant bits of my code:

Model:

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:

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):

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

<body>

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

<
div id="content">

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

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

<table cellpadding="5">

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

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

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

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

<
tr>
<
td colspan="2"><input type="submit" value="Submit" /></td>
</
tr>

</
table>

</
form>

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

</
div>

<? $this->load->view('footer.php'); ?>

Thanks! :)

Profile
 
 
Posted: 17 December 2007 03:55 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  3
Joined  10-26-2007

I am having this issue also, my drop down boxes are not functioning either.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 62663 Total Logged-in Users: 36
Total Topics: 77201 Total Anonymous Users: 1
Total Replies: 416766 Total Guests: 272
Total Posts: 493967    
Members ( View Memberlist )