I am developing a blog and in the registration form im trying to return the errors to the view with -almost- no luck!
Code:
function register()
{
if($_POST)
{
$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'trim|required|min_length[3]|is_unique[users.username]'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|min_length[5]'
),
array(
'field' => 'email',
'label' => 'E-mail',
'rules' => 'trim|required|valid_email|is_unique[users.email]'
)
);
$this->load->library('form_validation');
$this->form_validation->set_rules($config);
$data['errors'] = 0;
if($this->form_validation->run() == FALSE)
{
$data['errors'] = validation_errors();
}
else
{
$data = array(
'username' => $_POST['username'],
'email' => $_POST['email'],
'password' => $_POST['password']
);
$username = $data['username'];
$email = $data['email'];
$this->load->model('user');
$this->action_log->add_to_log("register", "New registration: $username - $email / SUCCESS");
$userid = $this->user->create_user($data);
}
}
$this->load->helper('form');
$this->load->view('__head__');
$this->load->view('register_user');
This code is working but the problem is when im trying to get back the validation errors. Somehow the guy in the tutorial made it work with pure magic. I tried to troubleshoot it and i came across with this solution:
Without doing anything im geting this warning. The code to display the errors:
Code:
<?php if($errors): ?>
<?= $errors ?>
<?php endif; ?>
My -almost- solution:
Since in the view im not passing any data i thought i would be a good idea to pass the $data variable.
Code:
$this->load->view('register_user', $data);
As a result im getting this warning
Severity: Notice
Message: Undefined variable: data
Filename: controllers/users.php
Line Number: 90
However its working:
So if the errors are not set im geting those warnings but when they are set everything works great.
How can i fix it?