Welcome Guest, Not a member yet? Register   Sign In
Validation Help Needed!!
#1

[eluser]yello[/eluser]
Hey there,

I want to use the validation class but I am having an error and I don't know what caused it.

Here is my controller:

Code:
<?php

class Register extends Controller {
    
    function Register()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
        
    }
    
    function index()
    {
        $this->load->library('validation');
        
        $rules['username'] = "trim|required|min_length[5]|max_length[12]|xss_clean";
        $rules['password'] = "trim|required|matches[passconf]|md5";
        $rules['passconf'] = "trim|required";
        $rules['email'] = "trim|required|valid_email";
        $rules['city'] = "trim|required";
        $rules['state'] = "trim|required";
        $rules['country'] = "trim|required";
        $rules['details'] = "trim|required";
        
        $this->validation->set_error_delimiters('<li style="color:red">', '</li>');
        $this->validation->set_rules($rules);
            
        if ($this->validation->run() == FALSE)
        {
            $query = $this->db->query('SELECT * FROM countries;');
            $data['countries'] = $query->result();

            $this->load->view('register/information', $data);
        }
        else
        {
            //do something with the vars
        }
    }
}
?&gt;

Here is my view:

Code:
&lt;?=$this->load->view('includes/header');?&gt;

&lt;?=$this->validation->error_string; ?&gt;
<h1>Modchip 411 Registration Form</h1>

&lt;?=form_open('register'); ?&gt;

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

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

<h5>City</h5>
&lt;input type="text" name="city" value="&lt;?=$this-&gt;validation->city;?&gt;" size="20" />

<h5>State/Province (if any)</h5>
&lt;input type="text" name="state" value="&lt;?=$this-&gt;validation->state;?&gt;" size="20" />

<h5>Country</h5>
<select type="text" name="country">
&lt;?php foreach($countries as $country):?&gt;

<option value="&lt;?=$country->name;?&gt;" &lt;?= $this->validation->set_select('country', '&lt;?=$country->name;?&gt;'); ?&gt; >&lt;?=$country->name;?&gt;</option>

&lt;?php endforeach;?&gt;
</select>

<h5>Service you offer</h5>

<h6>Modchips:</h6>
Xbox &lt;input type="checkbox" name="xbox" value="1" &lt;?= $this-&gt;validation->set_checkbox('xbox', '1'); ?&gt;/><br>
Xbox 360 &lt;input type="checkbox" name="xbox 360" value="1" &lt;?= $this-&gt;validation->set_checkbox('xbox 360', '1'); ?&gt;/><br>
PS1 &lt;input type="checkbox" name="ps1" value="1" &lt;?= $this-&gt;validation->set_checkbox('ps1', '1'); ?&gt;/><br>
PS2 &lt;input type="checkbox" name="ps2" value="1" &lt;?= $this-&gt;validation->set_checkbox('ps2', '1'); ?&gt;/><br>
PS3 &lt;input type="checkbox" name="ps3" value="1" &lt;?= $this-&gt;validation->set_checkbox('ps3', '1'); ?&gt;/><br>
Gamecube &lt;input type="checkbox" name="gamecube" value="1" &lt;?= $this-&gt;validation->set_checkbox('gamecube', '1'); ?&gt;/><br>
Wii &lt;input type="checkbox" name="wii" value="1" &lt;?= $this-&gt;validation->set_checkbox('wii', '1'); ?&gt;/><br>
PSP &lt;input type="checkbox" name="psp" value="1" &lt;?= $this-&gt;validation->set_checkbox('psp', '1'); ?&gt;/><br>

<h6>Other</h6>
Repairs &lt;input type="checkbox" name="repairs" value="1" &lt;?= $this-&gt;validation->set_checkbox('repairs', '1');?&gt;/><br>
Tuning &lt;input type="checkbox" name="tuning" value="1" &lt;?= $this-&gt;validation->set_checkbox('tuning', '1');?&gt;/><br>

<h5>Details</h5>
&lt;textarea name="details" rows="10" cols="70"&gt;&lt;?=$this->validation->details;?&gt;&lt;/textarea&gt;


<h5>Password</h5>
&lt;input type="password" name="password" value="" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="password" name="passconf" value="" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" style="font-size:150%"/&gt;</div>

&lt;/form&gt;

&lt;?=$this->load->view('includes/footer');?&gt;

I get bunch of notice like these:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: username

That error is repeated for each element in the form. Is this CI related bug? I can't find the damn problem!!

Thanks
#2

[eluser]llbbl[/eluser]
you forgot to set the $fields

Code:
unction 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);
        
    if ($this->validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}

http://ellislab.com/codeigniter/user-gui...ation.html




Theme © iAndrew 2016 - Forum software by © MyBB