[eluser]zamrg[/eluser]
I have a registration form which if successful, shows a view and calls a die() to prevent the script from continuing to show the form again (with any errors).
The problem occurs with the code segment
Code: $data['content'] = 'user/register_success';
$this->load->view('template', $data);
die();
I get no output if I keep the die(), however if I remove it, CI outputs both views (which is correct), but not what I'm looking for.
.. and here is a full copy of the register() function in my controller
Code: function register()
{
// redirect user to portal if already logged in
if (is_logged_in())
{
redirect('/user/portal');
}
$this->load->library('email');
$this->load->library('form_validation');
$this->load->helper('string');
$data['errors'] = array();
// check if form was submitted
if (count($_POST) > 0)
{
$this->form_validation->set_error_delimiters('<div class="form-error">', '</div>');
$this->form_validation->set_rules('username', 'lang:register.username', 'required|alpha_dash|min_length[' . $this->config->item('username_min_length') . ']|max_length[' . $this->config->item('username_max_length') . ']|callback__username_duplicate_check');
$this->form_validation->set_rules('email', 'lang:register.email', 'required|valid_email|max_length[120]|callback__email_duplicate_check');
$this->form_validation->set_rules('password', 'lang:register.password', 'required|min_length[' . $this->config->item('password_min_length') . ']|max_length[' . $this->config->item('password_max_length') . ']');
$this->form_validation->set_rules('confirm_password', 'lang:register.confirm_password', 'required|min_length[' . $this->config->item('password_min_length') . ']|max_length[' . $this->config->item('password_max_length') . ']|matches[password]');
// check if form data passes validation
if ($this->form_validation->run())
{
// generate a random activation code
$activation_code = random_string('alnum', 20);
// prepare data for database entry
$insert_data = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->_prep_password($this->input->post('password')),
'group_id' => $this->config->item('default_group_id'),
'activation_code' => $activation_code,
'join_date' => time()
);
// create user in database
$user_id = $this->user->create($insert_data);
// send activation e-mail to user
$email_data = array(
'username' => $this->input->post('username'),
'site_name' => $this->config->item('site_name'),
'activation_link' => site_url('user/activate_account/' . $user_id . '/' . $activation_code),
'support_email' => $this->config->item('site_support_email')
);
$this->email->from($this->config->item('site_noreply_email'), $this->config->item('site_name'));
$this->email->to($this->input->post('email'));
$this->email->subject(lang('email.activation.subject'));
$this->email->message_from_view('activation', $email_data);
if (!$this->email->send())
{
// failed to send activation e-mail
// TODO: log event
}
// show success page
$data['email'] = $this->input->post('email');
$data['support_email'] = $this->config->item('site_support_email');
$data['title'] = lang('register.title');
$data['content'] = 'user/register_success';
$this->load->view('template', $data);
die();
}
}
$data['title'] = lang('register.title');
$data['content'] = 'user/register';
$this->load->view('template', $data);
}
[eluser]Dyllon[/eluser]
Go review the form validation area of the user guide
http://ellislab.com/codeigniter/user-gui...ation.html
Specifically:
Code: <?php
class Form extends Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?>
[eluser]zamrg[/eluser]
I've read the form validation docs and class and I use it quite successfully in the above script.
The method I've chosen is: (as depicted by the above script)
if form submitted
-- if validation successfull
---- process data, show success, die
otherwise fall back and show form again
[eluser]Dyllon[/eluser]
You're checking the count of the $_POST array which is redundant because the form validation class does it for you.
Code: function register()
{
// redirect user to portal if already logged in
if (is_logged_in())
{
redirect('/user/portal');
}
$this->load->library('email');
$this->load->library('form_validation');
$this->load->helper('string');
$data['errors'] = array();
$this->form_validation->set_error_delimiters('<div class="form-error">', '</div>');
$this->form_validation->set_rules('username', 'lang:register.username', 'required|alpha_dash|min_length[' . $this->config->item('username_min_length') . ']|max_length[' . $this->config->item('username_max_length') . ']|callback__username_duplicate_check');
$this->form_validation->set_rules('email', 'lang:register.email', 'required|valid_email|max_length[120]|callback__email_duplicate_check');
$this->form_validation->set_rules('password', 'lang:register.password', 'required|min_length[' . $this->config->item('password_min_length') . ']|max_length[' . $this->config->item('password_max_length') . ']');
$this->form_validation->set_rules('confirm_password', 'lang:register.confirm_password', 'required|min_length[' . $this->config->item('password_min_length') . ']|max_length[' . $this->config->item('password_max_length') . ']|matches[password]');
// check if form data passes validation
if ($this->form_validation->run() == FALSE)
{
//Validation failed or form was not submitted, show form
$data['title'] = lang('register.title');
$data['content'] = 'user/register';
$this->load->view('template', $data);
}
else
{
//Validation passed... do stuff.
// generate a random activation code
$activation_code = random_string('alnum', 20);
// prepare data for database entry
$insert_data = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->_prep_password($this->input->post('password')),
'group_id' => $this->config->item('default_group_id'),
'activation_code' => $activation_code,
'join_date' => time()
);
// create user in database
$user_id = $this->user->create($insert_data);
// send activation e-mail to user
$email_data = array(
'username' => $this->input->post('username'),
'site_name' => $this->config->item('site_name'),
'activation_link' => site_url('user/activate_account/' . $user_id . '/' . $activation_code),
'support_email' => $this->config->item('site_support_email')
);
$this->email->from($this->config->item('site_noreply_email'), $this->config->item('site_name'));
$this->email->to($this->input->post('email'));
$this->email->subject(lang('email.activation.subject'));
$this->email->message_from_view('activation', $email_data);
if (!$this->email->send())
{
// failed to send activation e-mail
// TODO: log event
}
// show success page
$data['email'] = $this->input->post('email');
$data['support_email'] = $this->config->item('site_support_email');
$data['title'] = lang('register.title');
$data['content'] = 'user/register_success';
$this->load->view('template', $data);
}
}
[eluser]zamrg[/eluser]
ok I see, so there's no need to check if the form is actually submitted and only then setup validation rules and validate the input data.
I have modified my script and it obviously runs as intended since the die() is no longer a problem.
I'm still wondering why the die() halts execution of the output of the view when the view is called before the die(). Does this have to the with the CI output class and output buffering?
[eluser]Colin Williams[/eluser]
It's called output buffering. The loader withholds output until the controller is complete. When you die() in the controller, you never reach that point.
[eluser]zamrg[/eluser]
Thanks for all the assistance.
I'll mark this as solved.
|