[eluser]cobolCowboy[/eluser]
<br />
I'm a member of newbies a-go-go, CI daddio.
<br />
I've spent some time reading the user guide and many introductory tuts.
So many are the same, teach by example, however, I prefer an understanding that will serve me in numerous situations.
So here I have this FORM processing example taken right from the user guide
Code:
<?php
class Form extends Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
<br />
This works great in those cases where the form in blank to begin with, and we're entering new stuff either to login or to insert to the database. My needs are a little different whereas I'm pre-loading the form with data from the database for update purposes.
So here, we'll see an abbreviated version of my controller class
Code:
<?php
class Admin extends Controller {
function Admin()
{
parent::Controller();
}
function adv_update($id)
{
$data['doctitle'] = 'Admin Advertiser Maintenance';
$data['page'] = 'admin/adv_form';
$data['func'] = 'update';
$this->load->model('categories');
$data['categories'] = $this->categories->get_cats_all();
$this->load->model('adv_model');
$data['advertiser'] = $this->adv_model->get_one('id', $id);
$this->load->view('template', $data);
}
function adv_update_do()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('adv_network', 'Network', 'required');
$this->form_validation->set_rules('adv_id', 'Advertiser Id', 'trim|required|max-length[10]|xss_clean');
$this->form_validation->set_rules('adv_name', 'Advertiser Name', 'trim|required|max-length[100]|xss_clean');
$this->form_validation->set_rules('adv_logo', 'Advertiser Logo', 'trim|required|max-length[150]|xss_clean');
$this->form_validation->set_rules('adv_url', 'Advertiser URL', 'trim|required|max-length[150]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('template', $data);
}
else
{
foreach ($_POST as $key=>$value)
{
$data[$key] = $value;
}
$this->load->model('adv_model');
$this->adv_model->adv_update($data);
redirect('admin/adv_all');
}
}
}
?>
[b]My view
'template' knows which content view to load based on the value of $data['page'], so as you can see in
Code:
function adv_update($id)
the $id is passed in, retrieved from the database in the adv_model and then result is passed to the template view.
<br />
This all works like magic!!
<br />The form action POST goes to the
adv_update_do() function because if there is an error in the form, and it needs to be displayed again, I don't want to reload the form from the database again.
<br />
If there is no from error, the form data is passed to the adv_model/adv_update function where the update is done and the user is redirected to that function in the admin controller that puts up the list of advs in the database where another adv can be selected for update.
If there is an error, $this->form_validation->run() == FALSE is true , and it seems what was in the $data array is lost. I get a message from the admin controller that "data" is an indefined variable, and if that's true, it explains why the template doesn't know which content view to load.<br />
So, do I have to rebuild the $data array again but this time, of course, using the data from the $_POST array to make this work?
While we're here, the template contains a menu of categories that is database driven. so far I haven't grasped how to avoid having to retrieve a category array and pass in to the template for the category menu view without having to do this
Code:
$this->load->model('categories');
$data['categories'] = $this->categories->get_cats_all();
every time I load the template view. Anyone have a verbose answer in English as opposed to geek speak. CI daddio? Please.