Reputation:
I have 2 methods inside my controller, one is responsible for generating the signup page and another is responsible for handing the signup form submission.
<?php
class Signup_c extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['title'] = 'Sign Up';
$data['months'] = array(
'1' => 'January',
'2' => 'February',
'3' => 'March',
'4' => 'April',
'5' => 'May',
'6' => 'June',
'7' => 'July',
'8' => 'August',
'9' => 'September',
'10' => 'October',
'11' => 'November',
'12' => 'December'
);
$this->load->view('signup_v', $data);
}
function submit() {
// validation rules here...
// validate
if ($this->form_validation->run() === FALSE) {
$this->load->view('www/signup_v');
}
else {
// add info to database here...
$this->load->view('www/signup_success_v');
}
}
}
Now the problem is this, if there is a validation error then the user is returned back to the signup page and the validation errors are displayed. However no title or date is shown because those variables were defined inside the index() method and not the submit() method.
What is the best solution to this, I don't want to repeat my self and copy over those 2 variable declarations inside the signup method too. Is there a way to make it work in both method's views?
Upvotes: 0
Views: 227
Reputation: 9121
How about storing the data in a static array, if it's stays the same (i.e. is static)?
<?php
class Signup_c extends CI_Controller {
// private because no one else needs to access this
private static $data = array('title' => 'Sign Up', 'months' => array(...));
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('signup_v', Signup_c::$data);
}
function submit() {
// validation rules here...
// validate
if ($this->form_validation->run() === FALSE) {
$this->load->view('www/signup_v', Signup_c::$data);
}
else {
// add info to database here...
$this->load->view('www/signup_success_v'); // maybe add it here, too?
}
}
}
Alternatively, it might make sense to redirect the user back to the index. Unfortunately, I don't know much about CI, so I'm not the right person to give advice on this.
Another thing that comes to mind is: why do you need to have an array of months in your controller? I'm sure there's a better way to do this. One could eben consider to have it in the template. After all, months don't change or am I overlooking something?
Upvotes: 0
Reputation: 18833
Why not assign class variables? before construct:
$title = 'Whatever';
$months = array('january', 'february');
then inside your methods access them with $this->title and $this->months
Additionally, why not just post back to your index method and have it do post processing before anything else? so your index method begins with something like this:
if(!empty($_POST))
{
// do some kind of validation here
// or call a private method in this class and set your class vars accordingly
}
Upvotes: 1
Reputation:
You are able to save validation_errors();
in some variable, like sessions flashdata like:
$this->session->set_flashdata('errors', validation_errors());
and then echo them above your form (for example). It's better practice, I think.
BTW. you must load sessions module for this
Upvotes: 0