Welcome Guest, Not a member yet? Register   Sign In
CI Problem with form validation
#1

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:

[Image: 8zws9ti.png]
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:
[Image: am5FP19]
So if the errors are not set im geting those warnings but when they are set everything works great.
How can i fix it?
Reply


Messages In This Thread
CI Problem with form validation - by FlevasGR - 10-29-2014, 12:25 PM
RE: CI Problem with form validation - by ciadmin - 10-29-2014, 08:40 PM
RE: CI Problem with form validation - by FlevasGR - 10-30-2014, 04:33 AM
RE: CI Problem with form validation - by Narf - 10-31-2014, 11:49 AM
RE: CI Problem with form validation - by Avenirer - 10-30-2014, 05:56 AM
RE: CI Problem with form validation - by Avenirer - 10-30-2014, 06:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB