Welcome Guest, Not a member yet? Register   Sign In
Problem validating form data on a single 'page'
#1

[eluser]SSgt Dodger USMC[/eluser]
I have a HelpDesk program I am beginning to write in CI and I am having trouble creating a form that validates.

Before I begin, I am autoloading the following:
$autoload['libraries'] = array('database','session','email','validation','form_validation');
$autoload['helper'] = array('url','form','text','date','security','html');

I have a controller called helpdesk with a function called newnewticket that I want to invoke some views and collect information from brand new users on my intranet. Here is relevant part of the helpdesk controller:
Code:
class Helpdesk extends Controller {  // THIS CLASS NAME MUST BE CAPITALIZED
function newnewticket()
    {
        $data['main'] = 'content/newnewticket';
        $this->load->vars($data);
    $this->load->view('template'); //template view to load the constant page elements.
    }

Then I have a view called newnewticket that I want to present the user with a form to collect the data, and all display any validation errors encountered:

Code:
<?php
echo "<p>Before you can submit a ticket, we need to know some basic information in order to provide the best service.</p>";
echo "<p>All fields marked with an <span class='required'>*</span> are required!</p>";


//Show the validation errors received by the server (if any)
echo validation_errors();
echo $this->validation->error_string;

// assign css information to the form.
$attributes = array('class' => 'newuserform', 'id' => 'newuserform');
// open the form that will collect new user information
echo form_open('validation/newusercheck', $attributes);

//Define attributes for all of the input fields.
$input_lname = array(
                'name'        => 'lname',
                'id'          => 'lname',
                'maxlength'   => '50',
                'size'        => '50',
                'value'       => set_value($this->validation->lname), //???????
                'class'       => 'input1',
              );
$input_fname = array(
                'name'        => 'fname',
                'id'          => 'fname',
                'maxlength'   => '50',
                'size'        => '50',
                'value'       => set_value($this->validation->fname),//????????
                'class'       => 'input1',
              );
$input_email = array(
                'name'        => 'email1',
                'id'          => 'email1',
                'maxlength'   => '50',
                'size'        => '50',
                'value'       => set_value($this->validation->email1),//???????
                'class'       => 'input1',
echo form_fieldset('Identification Information');    //block the data to look better.
    echo form_label('Last Name:', 'lname');
    echo "<span class='required'>*</span>";
    echo form_input($input_lname);
    echo br();        // GET RID OF THIS
    echo form_label('First Name:', 'fname');
    echo "<span class='required'>*</span>";
    echo form_input($input_fname);
    echo br();        // GET RID OF THIS
    echo form_label('Email Address:', 'email1');
    echo "<span class='required'>*</span>";
    echo form_input($input_email);
echo form_fieldset_close();  // close the block.
echo form_submit('submitbutton', 'Submit New User Info');
echo form_close();

Then I have a validation controller with a newusercheck function:
Code:
class Validation extends Controller {  // THIS CLASS NAME MUST BE CAPITALIZED
function newusercheck()
    {
        // Set the validation rules.
        $rules['fname']="required";
        $rules['lname']="required";
        $rules['email1']="required|valid_email";
        
        //Load the validation rules array.
        $this->form_validation->set_rules($rules);

        // Set the field values to repopulate the form
        $fields['fname']='First Name';
        $fields['lname']= 'Last Name';
        $fields['email1']='Email Address';
        // Load the field values.
        $this->validation->set_fields($fields);


        // check if user form has been submitted properly
        if ($this->validation->run()==FALSE) // if the validation FAILS (i.e. the user missed something)
        {
        // redisplay user form and repopulate fields
            $this->load->view('content/newusererror');
        }
        else  // go to next page
        {
            $this->load->view('whatever');
        }
    }

When I run this, it will take me back to a blank form on the newnewticket page, and displays no error message. It doesn't matter what I fill in or if it is valid or not.

AM I doing this right? Is there a simpler/better way? I have read the user manual on forms and form validation but I am stuck...

I am new to CI, and I would greatly appreciate any help or guidance.

Semper Fidelis,
-Dodger
#2

[eluser]pistolPete[/eluser]
You mix up the two libraries Validation and Form Validation, just use the latter!
From the user guide:
Quote:As of CodeIgniter 1.7.0, this Form Validation class supercedes the old Validation class, which is now deprecated. We have left the old class in the library so applications currently using it will not break, but you are encouraged to migrate to this new version.

In order to repopulate the form, use
Code:
set_value('field_name')
instead of
Code:
set_value($this->validation->field_name)

Have a look at the form validation tutorial again!
#3

[eluser]SSgt Dodger USMC[/eluser]
Thank you pistolPete!!

I was mixing them up, and I didn't even see it. It's working great now!




Theme © iAndrew 2016 - Forum software by © MyBB