Welcome Guest, Not a member yet? Register   Sign In
Forcing set_value to take DB data on a form.
#1

[eluser]gh0st[/eluser]
A page holds a resort editor which I will use for both ADD and EDIT actions.

However I'm stuck on something -- how do I force resortTitle in my example below to take another value.

Basically what I want to do is;
ie;
1. User adds an new resort (*done*)
2. User wants to edit a new resort and this info is pulled from DB
3. Force the DB stuff over the top of the contents of set_value
4. Continue using validation rules for error catching.

Code:
// controller
if ($action == 'edit')
{
   $data['id'] = (int) $this->uri->segment(4);

// the following line doesn't work, but this is an example of what I'm trying achieve.
   $this->form_validation->set_value("resortTitle", "Hello world");

// do the view
   $this->load->view("example");
}

Code:
// HTML View (example)
<input type="text" id="resortTitle" name="resortTitle" value="<?=set_value('resortTitle');?>" />
#2

[eluser]TheFuzzy0ne[/eluser]
set_value() should ideally be called from within your view, and as set_value() is in the form helper (which is loaded automatically by the form validation library), you don't need to bother with $this->form_validation->set_value(). Also, the standalone set_value() function extends the one in the form validation class. It's simply a shorthand version that does the same thing, however, it returns a value, it's not used to set anything as such. Perhaps it would have been better named get_value()?

The default value is only used if validation has not yet run, so you'd simply pass the default database test over into your view, and use it within set_value().

How this helps.
#3

[eluser]gh0st[/eluser]
I've figured out a solution.

As I'm only working with 2 fields my solutions was pretty straight forward, but I worry about larger forms.

What I did was to force the view to accept an array called data, in which I set the fields for the resortTitle, resortContent, etc.

So for an "add" action they'd all be blank, and for "edit" action they'd be filled in from the DB source;

ie;

Code:
// controller (pseudo-code)
if ($action == 'add') {
  $data['resortTitle']    = '';
  $data['resortContent']= '';
} elseif ($action == 'edit') {
  $data['resortTitle']    = 'Some title';
  $data['resortContent'] = 'Some content';
}

Code:
// view
<input type="text" id="resortTitle" name="resortTitle" value="<?=set_value('resortTitle', $resortTitle);?>" />

That's how I resolved the problem, although I do worry about bigger forms because that'd be a lot of info to check.
#4

[eluser]TheFuzzy0ne[/eluser]
If you have bigger forms then it would probably be wise to either break your method up into separate methods (if possible), and/or export some code from out of the main function. You could then use these functions as helpers to set your vairable defaults. For example:

resorts.php]
Code:
class Resorts extends Controller {

    function Resorts()
    {
        parent::Controller();
    }

    function index($action)
    {
        $data = array();
        // Some CI-exy CI logic.
        
        $data['defaults'] = ($action == "add") ? $this->_default_add_data() : $this->_default_edit_data();
        $this->load->view('some_view', $data);
    }

    function _default_edit_data()
    {
        // Do the database query.
        
        // Set output array.
        
        // Return the array by reference
    }

    function _default_add_data()
    {
        // Set output array.
        
        // Return the array by reference
    }
}
The code above is untested

At the end of the day, if the defaults need setting, then they need setting; coding for this is unavoidable.
#5

[eluser]pistolPete[/eluser]
I'd rather extend the form validation library.

MY_form_validation.php

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

    function set_defaults($fields)
    {
        if (!is_array($fields))
            return FALSE;

        if(!empty($_POST))
            return FALSE;

        foreach ($fields as $key => $value)
        {
            $this->_field_data[$key]['postdata'] = $value;
        }
    }
}

data_model.php
Code:
function get_entry($id = NULL)
{    
    if($id != NULL)
    {
        $this->db->where('id', $id);
        $this->db->limit('1');
        $query = $this->db->get($this->table);
        if( $query->num_rows() == 1 )
        {
            return $query->row_array();
        }
    }            
    return FALSE;
}

some_controller.php
Code:
function edit($id)
{
    $defaults = $this->data_model->get_entry($id);
    $this->form_validation->set_defaults($defaults);
    $this->layout->view('some_view');
}

some_view.php
Code:
<input type="text" id="some_id" name="some_name" value="<?=set_value('some_name');?>" />


I'd really love to see a functionality like this in the core library without the need to extend it...
#6

[eluser]Samuurai[/eluser]
[quote author="pistolPete" date="1234922282"]
data_model.php
Code:
function get_entry($id = NULL)
{    
    if($id != NULL)
    {
        $this->db->where('id', $id);
        $this->db->limit('1');
        $query = $this->db->get($this->table);
        if( $query->num_rows() == 1 )
        {
            return $query->row_array();
        }
    }            
    return FALSE;
}

I know this thread has been dormant for a while, but I'm just hoping someone can tell me what $this->table refers to above? Usually, I just type the table name there.
#7

[eluser]pistolPete[/eluser]
@Samuurai:

$this->table is just a class variable which represents the database table the model is accessing.
#8

[eluser]Samuurai[/eluser]
So how do you specify what it will be and pass it to the function?
#9

[eluser]pistolPete[/eluser]
Example:
Code:
class Data_model extends Model {

    var $table = 'insert_table_name_here';

    function get_entry($id = NULL)
    {    
        ...
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB