Welcome Guest, Not a member yet? Register   Sign In
noob - forms validation and set fields problem
#1

[eluser]nevsie[/eluser]
I have created a simple view and controller for username and password basic checks. At this stage i am just requiring the two fields, nothing complicated.
When i first load the page i get an error "Undefined property: username" which relates to the "$this->validation->username" part i load in as the input field value. ignoring this as i kind of expect this error seeing as there is no value that has been validated yet!!!

My main point and query at this stage is that in the controller i have set_fields for username and password, yet when i dump the $this array and dig through the relevant part, the validation _fields array is empty? Is this correct? I would expect it to have somehting in their either on refresh, or form being submitted.

Similarly, no matter what i do the validate error_string never gets a value into it. So even though i say required, and validation detects FALSE, no error string is returned?

i have tried a few things from the forums with no success... so i am guessing there is soehting i have just overlooking with my inexperience. Hop you can help.

Code:
<?php
class Login extends Controller {
    
    function Login()
    {
        parent::Controller();
        $this->load->library('validation');
        $this->load->helper('form');
    }

    function index()
    {
        $login_form_fields = array
        (
            'username' => 'Username',
            'password' => 'Password'
        );
        $this->validation->set_fields($login_form_fields);
        
        $login_form_rules = array
        (
            'username' => 'required',
            'password' => 'required'
        );
        $this->validation->set_rules($login_form_rules);
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('admin/login');
        }
        else
        {
            redirect('user');
        }
    }
}
?>

Code:
<h1>Login</h1>

&lt;?php echo $this->validation->error_string; ?&gt;

&lt;? echo form_open('login'); ?&gt;
    <p>Login Details</p>
    <p>Username:<br />&lt;? echo form_input(array('name' => 'username', 'id' => 'username', 'value' => $this->validation->username)); ?&gt;</p>
    <p>Password:<br />&lt;? echo form_input(array('name' => 'password', 'id' => 'password')); ?&gt;</p>
    <p>&lt;? echo form_submit('submit', 'Submit'); ?&gt;</p>
&lt;? echo form_close(); ?&gt;

<pre>
&lt;? print_r($this); ?&gt;
</pre>
#2

[eluser]Seppo[/eluser]
Which PHP and CI version are you running?
#3

[eluser]nevsie[/eluser]
Hi, Thanks for the reply, i am using php version 4.4.8 and what appears to be the latest version of CI 1.6.1

Any help is more than appreciated. I know this will be a simple stumbling block that is most likely related to the php version... But all the changes i have tried do not appear to work.
#4

[eluser]Seppo[/eluser]
There was a bug in CI about this, but I thought it was fixed on 1.6...
Quick fix: move $this->load->library('validation'); from the constructor to the index method.
I'll try to find out a better solution later...
#5

[eluser]nevsie[/eluser]
hi and thanks for that... you know i had seen that bug and thought i had tried moving it... However, i now realise that i had duplicated "$this->load->library(’validation’);" in both the constructor and the index method. When duplicated it still throws up the error... when cut out and pasted in the index method only it works.

But cheers for the support. N
#6

[eluser]zenonn[/eluser]
Getting Undefined property: CI_Validation:: errors. I upgraded to 1.6.2 and my forms are having all types of validation problems.

The code:

Thanks in advance.

&lt;?php
class Customer extends Controller
{
function __construct(){
parent::Controller();
$this->load->model('quote_model');

}








function index()
{

$head['title'] = "Customer Online Quote";
$this->load->helper(array('form', 'url'));

$this->load->library('validation');

$rules['fname'] = "required|max_length[40]";
$rules['lname'] = "required|max_length[40]";
$rules['email'] = "required|max_length[40]|valid_email";

$this->validation->set_error_delimiters('<div class="error">', '</div>');
$this->validation->set_rules($rules);

$fields['fname'] = 'First Name';
$fields['lname'] = 'Last Name';
$fields['email'] = 'Email Address';

$this->validation->set_fields($fields);


$data['fname'] = $this->input->post('fname');
$data['lname'] = $this->input->post('lname');
$data['email'] = $this->input->post('email');
$data['generator'] = $this->input->post('quote');
$data['code'] = rand(10000,500000);
$data['call'] = $this->input->post('mail');





if ($this->validation->run() == FALSE)
{
$this->load->view('cquote',$head);
}
else
{ $this->quote_model->save_data($data);
redirect('customer/success');

}
}

The Form

&lt;?=form_label('First Name:*','First Name');?&gt;

&lt;input type="text" name="fname" value="&lt;?=$this-&gt;validation->fname;?&gt;"/>

&lt;?=$this->validation->fname_error;?&gt;

</li>
<li>
&lt;?=form_label('Last Name:*','Last Name');?&gt;

&lt;input type="text" name="lname" value="&lt;?=$this-&gt;validation->lname;?&gt;"/>
&lt;?=$this->validation->lname_error;?&gt;
</li>
<li>
&lt;?=form_label('Email:*','Email');?&gt;
&lt;input type="text" name="email" value="&lt;?=$this-&gt;validation->email;?&gt;"/>

&lt;?=$this->validation->email_error;?&gt;
</li>

</ol>
</fieldset>
<fieldset>

<legend>Quote</legend>
<ol>
<li>
&lt;?=form_label('Generators:','Generators');?&gt;
<select name="quote">
<option value="8kw" selected="selected">8,000 Watts</option>
<option value="10kw">10,000 Watts</option>
<option value="14kw">14,000 Watts</option>
<option value="17kw">17,000 Watts</option>
<option value="20kw">20,000 Watts</option>

</select>
</li>
<li>
Would you like a follow up email?</li>
<li>&lt;?=form_label('Please Select:','Please Select');?&gt;

<select name="mail">
<option value="No" selected="selected">No</option>
<option value="Yes">Yes</option>


</select>
</li>


</ol>
</fieldset>

<fieldset class="submit">
&lt;input type="submit" name="submit" value="submit" class="submit"/&gt;
</fieldset>
&lt;?=form_close();?&gt;
#7

[eluser]zenonn[/eluser]
The problem has been solved from another thread thanks
#8

[eluser]soupdragon[/eluser]
might be helpful to put a link to that thread then :-)




Theme © iAndrew 2016 - Forum software by © MyBB