CodeIgniter Forums
validation not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: validation not working (/showthread.php?tid=12598)



validation not working - El Forum - 10-24-2008

[eluser]smick[/eluser]
Greetings,

I'm experiencing a PHP error when using CI's validation class. Unable to track the error down, I did a fresh install of CI (version: 1.6.3) and used minimal code to test validation yet still I receive error. The error is question is:

Quote:Severity: Notice

Message: Undefined property: CI_Validation::$username

My test code was swiped from the documentation and is as follows:

Controller:
Code:
<?php

class Test_validation extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('validation');
                
        $rules['username']    = "required";
        $rules['password']    = "required";
        $rules['passconf']    = "required";
        $rules['email']        = "required";
        
        $this->validation->set_rules($rules);
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('test_validation');
        }
        else
        {
            $this->load->view('test_validation');
        }
    }
}
?>

View:
Code:
<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo $this->validation->error_string; ?>

<?php echo form_open('test_validation'); ?>

<h5>Username</h5>
&lt;input type="text" name="username" value="&lt;?php echo $this-&gt;validation->username;?&gt;" size="50" />

<h5>Password</h5>
&lt;input type="text" name="password" value="&lt;?php echo $this-&gt;validation->password;?&gt;" size="50" />

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="&lt;?php echo $this-&gt;validation->passconf;?&gt;" size="50" />

<h5>Email Address</h5>
&lt;input type="text" name="email" value="&lt;?php echo $this-&gt;validation->email;?&gt;" size="50" />

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

I have successfully used CI's validation class in the past. Might my host have changed something on me?

Also, I'm working on a super clean and super easy to implement user auth model which I hope to share with the community when complete. What would be the proper channel through which to submit this?

Thanks for the help,

Nathan


validation not working - El Forum - 10-24-2008

[eluser]Adam Griffiths[/eluser]
I would advise you to upgrade to CI 1.7 and use the much better form validation class.


validation not working - El Forum - 10-24-2008

[eluser]Colin Williams[/eluser]
You forgot to define fields. All you defined were rules. That is why there is no 'username' property.


validation not working - El Forum - 10-24-2008

[eluser]Aken[/eluser]
[quote author="Colin Williams" date="1224919800"]You forgot to define fields. All you defined were rules. That is why there is no 'username' property.[/quote]
Yeah, in 1.6.3 you need to define a human-name for the field using something like $this->validation->set_field('user') = "Username" (if I recall correctly).

However as mentioned, CI 1.7 was just released and has a much nicer validation class.


validation not working - El Forum - 10-24-2008

[eluser]Colin Williams[/eluser]
Yeah, because setting a function call equal to a string was the way CI 1.6.3 worked (or didn't)... Not sure how they pulled that one off...

1.7's Form Validation class probably would circumvent this mistake because rules and fields are defined as one:

Code:
$this->form_validation->set_rules('username', 'Username', 'required');

But just saying "use 1.7 and your problems will go away" is misleading and incorrect.


validation not working - El Forum - 10-24-2008

[eluser]Aken[/eluser]
Bah, it initialized an array, not the way I wrote it (bad memory, lol).

For 1.6.3, you need to define field names like so:
Code:
$fields['username'] = 'User';
$fields['email'] = 'Contact Email';
$fields['message'] = 'Message';
        
$this->validation->set_fields($fields);



validation not working - El Forum - 10-25-2008

[eluser]Adam Griffiths[/eluser]
@colin: I merely said to upgrade to the form validation class in 1.7 because it is much nicer, and having read the other posts in this thread and seen how easy it is to fix the problem in 1.7.


validation not working - El Forum - 10-25-2008

[eluser]smick[/eluser]
Great, problem was missing field variables.
Looks like CI switched the docs up on me as well.

Thanks all!

Nathan