CodeIgniter Forums
Validation fails to fill form elements or error message. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Validation fails to fill form elements or error message. (/showthread.php?tid=10508)

Pages: 1 2


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]valarkin[/eluser]
I am working on a reservation tool and am having problems with the validation. When I validate the information it fails and calls the load view method. However, none of the fields populate, nor does the error message populate.If I echo the error field before loading the view I can see all the errors.

Any ideas what might be causing this?

Here is some code of the code:

(Controller Function that handles Validation)
Code:
function _validateBirthdayReservation() {    
        
        $rules['conFirstName']        = "trim|required";
        $rules['conLastName']        = "required";
        $rules['conAddress']        = "required";
        $rules['conCity']            = "required";
        $rules['conState']            = "required";
        $rules['conZip']            = "required";        
        $rules['conEMail']            = "required";
        $rules['conPhone']            = "required";
        $rules['childFirstName']    = "required";
        $rules['childLastName']        = "required";
        $rules['childDOBDay']        = "required";
        $rules['childDOBMonth']        = "required";
        $rules['childDOBYear']        = "required";
        $rules['childFavColor']        = "required";
        $rules['partyDateDay']        = "required";
        $rules['partyDateMonth']    = "required";
        $rules['partyDateYear']        = "required";
        $rules['partyTime']            = "required";
        $rules['partySodaType']        = "required";
        $rules['partyPizzaType']    = "required";
        $rules['partyNbrKids']        = "required";
        $rules['ccHolderName']        = "required";
        $rules['ccType']            = "required";
        $rules['ccNumber']            = "required";
        $rules['ccExpDateMonth']    = "required";
        $rules['ccExpDateYear']        = "required";

        $this->validation->set_rules($rules);
        
        $fields['conFirstName']        = "Contact First Name";
        $fields['conLastName']        = "Contact Last Name";
        $fields['conAddress']        = "Contact Address";
        $fields['conCity']            = "Contact City";
        $fields['conState']            = "Contact State";
        $fields['conZip']            = "Contact Zip";    
        $fields['conEMail']            = "Contact E-Mail";
        $fields['conPhone']            = "Contact Phone";
        $fields['childFirstName']    = "Child's First Name";
        $fields['childLastName']    = "Child's Last Name";
        $fields['childDOBDay']        = "Child's Date of Birth: Day";
        $fields['childDOBMonth']    = "Child's Date of Birth: Month";
        $fields['childDOBYear']        = "Child's Date of Birth: Year";
        $fields['childFavColor']    = "Child's Favorit Color";
        $fields['partyDateDay']        = "Party Date: Day";
        $fields['partyDateMonth']    = "Party Date: Month";
        $fields['partyDateYear']    = "Party Date: Year";
        $fields['partyTime']        = "Party Time";
        $fields['partySodaType']    = "Party Soda Type";
        $fields['partyPizzaType']    = "Party Pizza Type";
        $fields['partyNbrKids']        = "Number of Kids";
        $fields['ccHolderName']        = "Credit Card Holder Name";
        $fields['ccType']        = "Credit Card Type";
        $fields['ccNumber']            = "Credit Card Number";
        $fields['ccExpDateMonth']    = "Credit Card Expiration: Month";
        $fields['ccExpDateYear']    = "Credit Card Expiration: Year";
        
        $this->validation->set_fields($fields);
        
        if (!$this->validation->run()) {
            //echo $this->validation->error_string;
            $this->load->view('parties_birthday_reservation');
        } else {
            //$this->load->view('formsuccess');
            echo "The form passed all test!";
        }
    }

(A part of the View 'parties_birthday_reservation')

Code:
<label id="conZip-label">Zip:
        &lt;input id="conZip-input" name="conZip" type="text" value="&lt;?=$this-&gt;validation->conZip;?&gt;">
        </label>
        <label id="conEMail-label">E-Mail Address:
        &lt;input id="conEMail-input" name="conEMail" type="text" value="&lt;?=$this-&gt;validation->conEMail;?&gt;">
        </label>
        <label id="conPhone-label">Phone Number:
        &lt;input id="conPhone-input" name="conPhone" type="text" value="&lt;?=$this-&gt;validation->conPhone;?&gt;">
        </label>

(Another part of the View 'parties_birthday_reservation' that handles the error information)
Code:
&lt;?php if ($this->validation->error_string <> ''): ?&gt;
      <div style="float:left; margin-left:10px; width: 310px; padding:10px; border: solid 1px #FF0000; margin-top:10px; color:#FF0000;">
      Error(s): <br />
      &lt;?=$this->validation->error_string;?&gt;
      </div>
      &lt;?php endif; ?&gt;

Any thoughts?


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]Yash[/eluser]
Quote:I am working on a reservation tool and am having problems with the validation. When I validate the information it fails and calls the load view method. However, none of the fields populate, nor does the error message populate.If I echo the error field before loading the view I can see all the errors.

Show me error...


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]valarkin[/eluser]
There are no "errors" so to speak. The problem is that the validation data is not getting to the view. From what I have seen, I don't need to explicitly pass the validation data to the view. The validation library should have the data accessible through $this->validation->{variable}. However, this is not happening. $this->validation->{variable} is returning nil when called from the 'parties_birthday_reservation' view.


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]Yash[/eluser]
Load validation library ?

There will be not data untill you submit the form.

In your view file I can't any Form?Please show full view and controller.


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]valarkin[/eluser]
[quote author="Yash" date="1217722241"]Load validation library ?[/quote]

Code:
function Parties()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');
        $this->output->enable_profiler(TRUE);
    }

[quote author="Yash" date="1217722241"]There will be not data untill you submit the form.[/quote]

The form has been submitted. Profiler shows me all the posted data, and the data IS being validated. However, when I call:
Code:
$this->load->view('parties_birthday_reservation');

The variables: $this->validate->conFirstName contains nothing.

[quote author="Yash" date="1217722241"]In your view file I can't any Form?Please show full view and controller.[/quote]

Here is the Controller:
Code:
&lt;?php

class Parties extends Controller {

    function Parties()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');
        $this->output->enable_profiler(TRUE);
    }
    
    function index()
    {
        $this->load->view('parties');
    }
    
    function birthday_reservation() {
    
        $action = $this->uri->segment(3);
        
        switch ($action) {
            case "submit":
                $this->_validateBirthdayReservation();
            break;
            case "form":                
                $this->load->view('parties_birthday_reservation');
            break;
        }
    }
    
    function _validateBirthdayReservation() {    
        
        $rules['conFirstName']        = "trim|required";
        $rules['conLastName']        = "required";
        $rules['conAddress']        = "required";
        $rules['conCity']        = "required";
        $rules['conState']        = "required";
        $rules['conZip']        = "required";        
        $rules['conEMail']        = "required";
        $rules['conPhone']        = "required";
        $rules['childFirstName']    = "required";
        $rules['childLastName']        = "required";
        $rules['childDOBDay']        = "required";
        $rules['childDOBMonth']        = "required";
        $rules['childDOBYear']        = "required";
        $rules['childFavColor']        = "required";
        $rules['partyDateDay']        = "required";
        $rules['partyDateMonth']    = "required";
        $rules['partyDateYear']        = "required";
        $rules['partyTime']        = "required";
        $rules['partySodaType']        = "required";
        $rules['partyPizzaType']    = "required";
        $rules['partyNbrKids']        = "required";
        $rules['ccHolderName']        = "required";
        $rules['ccType']        = "required";
        $rules['ccNumber']        = "required";
        $rules['ccExpDateMonth']    = "required";
        $rules['ccExpDateYear']        = "required";

        $this->validation->set_rules($rules);
        
        $fields['conFirstName']        = "Contact First Name";
        $fields['conLastName']        = "Contact Last Name";
        $fields['conAddress']        = "Contact Address";
        $fields['conCity']        = "Contact City";
        $fields['conState']        = "Contact State";
        $fields['conZip']        = "Contact Zip";    
        $fields['conEMail']        = "Contact E-Mail";
        $fields['conPhone']        = "Contact Phone";
        $fields['childFirstName']    = "Child's First Name";
        $fields['childLastName']    = "Child's Last Name";
        $fields['childDOBDay']        = "Child's Date of Birth: Day";
        $fields['childDOBMonth']    = "Child's Date of Birth: Month";
        $fields['childDOBYear']        = "Child's Date of Birth: Year";
        $fields['childFavColor']    = "Child's Favorit Color";
        $fields['partyDateDay']        = "Party Date: Day";
        $fields['partyDateMonth']    = "Party Date: Month";
        $fields['partyDateYear']    = "Party Date: Year";
        $fields['partyTime']        = "Party Time";
        $fields['partySodaType']    = "Party Soda Type";
        $fields['partyPizzaType']    = "Party Pizza Type";
        $fields['partyNbrKids']        = "Number of Kids";
        $fields['ccHolderName']        = "Credit Card Holder Name";
        $fields['ccType']        = "Credit Card Type";
        $fields['ccNumber']        = "Credit Card Number";
        $fields['ccExpDateMonth']    = "Credit Card Expiration: Month";
        $fields['ccExpDateYear']    = "Credit Card Expiration: Year";
        
        $this->validation->set_fields($fields);
        
        if (!$this->validation->run()) {
            echo $this->validation->error_string;
            echo $this->validation->conFirstName;
            $this->load->view('parties_birthday_reservation');
        } else {
            //$this->load->view('formsuccess');
            echo "The form passed all test!";
        }
    }
}
?&gt;

There isn't enough space for the view. But everything is done the same way as the section that was in my first post.


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]Yash[/eluser]
In your first post main form_open() is missing I'm looking for that.
It can look stupid to you but believe me people do that...


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]valarkin[/eluser]
Code:
&lt;?=form_open('parties/birthday_reservation/submit', array('name' => 'birthday_reservation'));?&gt;

Only problem I have is that the dang validation variables are not repopulating the form fields.


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]Yash[/eluser]
It automatically done when you submit form and get any error in form like [required element is missing] then form repopulate with old values.If there is error I can correct it.

I'm getting where you have error.Sorry can't help much.

Just try with some different test form with less complex structure for starting.


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]valarkin[/eluser]
I found the issue. I was calling $this->load->library('validation'); in the constructor. You need to have it in the method that you will be calling $this->load->view() from!

Ah well, live and learn!


Validation fails to fill form elements or error message. - El Forum - 08-02-2008

[eluser]Yash[/eluser]
[quote author="valarkin" date="1217727988"]I found the issue. I was calling $this->load->library('validation'); in the constructor. You need to have it in the method that you will be calling $this->load->view() from!

Ah well, live and learn![/quote]

Just use autoloader library in config and save tons of times.Glad you got it.