Welcome Guest, Not a member yet? Register   Sign In
Form Validation from User Guide does not seem to work
#1

[eluser]Ki[/eluser]
I have looked through the code a million times - its a cut and paste directly from User Guide Validation, but does not seem to work. I get no errors, just reloading of the form:

Controller - Form.php
Code:
<?php

class Form 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);
    
    $fields['username']    = 'Username';
    $fields['password']    = 'Password';
    $fields['passconf']    = 'Password Confirmation';
    $fields['email']    = 'Email Address';

    $this->validation->set_fields($fields);
        d($this->validation->run());
    if ($this->validation->run() == FALSE)
    {

        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}
}
?>


From - MyForm

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

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

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

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

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

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

<h5>Email Address</h5>
&lt;?php echo $this->validation->email_error; ?&gt;
&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;
#2

[eluser]Ki[/eluser]
does anybody have an example of a working form validation?
#3

[eluser]Mike Ryan[/eluser]
Hi,

The example in the docs does work, so something went wrong during the copy and paste. I can see some incorrect code in what you posted:
Code:
rules['Username']    = "required";
//should be:
    $rules['username']    = "required";
//...
    $this->validation->set_fields($fields);
        d($this->validation->run()); //this line should be removed
    if ($this->validation->run() == FALSE)

I would suggest deleting it all, and starting the test again. Also, you should be using the new Form Validation instead of the old Validation class.




Theme © iAndrew 2016 - Forum software by © MyBB