• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

#2
This should probably be in the Issues area. Just sayin'
Reply

#3
An undefined variable error message, simply tells you that the variable you are trying to use is undefined!


You've done something like this

PHP Code:
if (something_that_might_happen === TRUE)
{
  
$message "Wow it worked"!
}

echo 
$message

So $message ONLY Exists - ie is defined if your IF statement is TRUE... When it's not, $message does not exist!


So you need to ensure that if you are going to use a variable anywhere, that it has to be SET to something!

PHP Code:
$message '';
if (
$something_that_might_happen === TRUE)
{
  
$message "Wow it worked"!
}
echo 
$message

or
PHP Code:
if ($something_that_might_happen === TRUE)
{
  
$message "Wow it worked!";
}
else
{
  
$message "Well that didn't work";
}
echo 
$message

And then you'd ask - Where does $something_that_might_happen get set?

PHP Code:
if (isset($something_that_might_happen) AND $something_that_might_happen === TRUE)
{
  
$message "Wow it worked!";
}
else
{
  
$message "Well that didn't work";
}
echo 
$message

And if you are using $_POST or _GET and any array and using those to test or display. Do they exist?

So really it's a matter of - "Oh I get an undefined variable or index or whatever.. Is it defined when it's being used?"

Hope that helps you fix your issue!
Reply

#4
(10-29-2014, 08:40 PM)ciadmin Wrote: This should probably be in the Issues area. Just sayin'

Oups. My mistake Tongue Can you move it?
Reply

#5
You loaded the view without transfering the $data to it... Should be: $this->load->view('register_user', $data);
Reply

#6
Don't know if this forum works (I already have a post aproved but my reply to your problem didn't appear), but regarding your problem, you only defined the $data after someone did a POST. So, when the view is loaded for the first time (with GET), there is no $data defined.
Reply

#7
(10-29-2014, 08:40 PM)ciadmin Wrote: This should probably be in the Issues area. Just sayin'

No, it should not be in the Issues forum.

Development / Issues is about possible bugs, and this thread has nothing to do with that.
Reply

#8
@FlevasGR

Pay attention on what @Avenirer wrote. It is not clear why you have chosen to activate the entire code only for POST request. $this->form_validation->run() makes its check only for POST requests.

It is possible validation rules to depend on the input POST data and they to need some modifications. Other approach for the same effect is defining special callback rules. But this is not your case. You can remove the if($_POST) check.

Anyway, in CodeIgniter 3 if you really need check explicitly for POST request, you may write if ($this->input->method() == 'post')
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.