Welcome Guest, Not a member yet? Register   Sign In
Issue with Validation library
#1

[eluser]awpti[/eluser]
Okay, running the latest - 1.6.3

Problem: http://ignitedjobs.com/addjob/

Explanation:
Following the examples in the User Guide results in notices that $this->validation->...var here... is not set. This goes away if I submit with even just one field filled in.

I tried to avoid it by abusing ternary operators (yuck to that attempt), but I just can't figure out the cause. According to the UG, this is how it should be done.

The Controller:

Code:
function validate()
        {
                $this->view->part('header', 'tpl/header');
                $this->view->part('footer', 'tpl/footer');

                // Set the validation rules.
                $rules = array
                (
                        'location'      => 'required|xss_clean',
                        'company'       => 'xss_clean',
                        'email'         => 'required|valid_email',
                        'job_title'     => 'required|xss_clean',
                        'description'   => 'required|xss_clean',
                        'to_apply'      => 'required|xss_clean',
                        'website'       => 'required|prep_url|xss_clean'
                );

                $fields = array
                (
                        'location'      => 'Location',
                        'company'       => 'Company',
                        'email'         => 'E-Mail',
                        'job_title'     => 'Job Title',
                        'description'   => 'Description',
                        'to_apply'      => 'How to Apply',
                        'website'       => 'Website'
                );

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

                if ($this->validation->run() == FALSE)
                {
                        $this->view->load('add_job');
                }
                else
                {
                        redirect('/addjob/accepted', 'header');
                }
        }

The View:

Code:
if($this->validation->error_string) { echo '<div class="notlive"><p>'.$this->validation->error_string.'</p></div>'; }

echo form_open('addjob/validate', array('id' => 'jobs', 'class' => 'cssform'));

echo '<p><label for="email"><span style="color: red">E-Mail</span>:</label>';
echo form_input(array('id' => 'email', 'name' => 'email', 'value' => $this->validation->email));
echo '<br />Required to edit entry.';
echo '</p>';

echo '<p><label for="company">Company Name:</label>';
echo form_input(array('id' => 'company', 'name' => 'company', 'value' => $this->validation->company));
echo '<br />Example: Ignited Jobs, Inc';
echo '</p>';

echo '<p><label for="job_title"><span style="color: red">Job Title</span>:</label>';
echo form_input(array('id' => 'job_title', 'name' => 'job_title', 'value' => $this->validation->job_title));
echo '<br />Example: Need an experienced Developer!';
echo '</p>';

echo '<p><label for="location"><span style="color: red">Location</span>:</label>';
echo form_input(array('id' => 'location', 'name' => 'location', 'value' => $this->validation->location));
echo '<br />Example: Phoenix, AZ or Freelance/Remote';
echo '</p>';

echo '<p><label for="website"><span style="color: red">Website</span>:</label>';
echo form_input(array('id' => 'website', 'name' => 'website', 'value' => $this->validation->website));
echo '<br />Example: http://ignitedjobs.com';
echo '</p>';

echo '<p><label for="description"><span style="color: red">Description</span>:</label>';
echo form_textarea(array('id' => 'description', 'name' => 'description', 'rows' => 20, 'cols' => '40', 'value' => $this->validation->description));
echo '</p>';

echo '<p><label for="to_apply"><span style="color: red">How to Apply</span></label>';
echo form_input(array('id' => 'to_apply', 'name' => 'to_apply', 'value' => $this->validation->to_apply));
echo '<br />Example: E-Mail or URL (Contact info!)';
echo '</p>';

echo '<div style="margin-left: 162px;">';
echo form_submit('add', 'Add this Job!');
echo '</div>';
echo form_close();
#2

[eluser]Yash[/eluser]
give me correct error message
#3

[eluser]awpti[/eluser]
You can see it right off the linked site.

A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Validation::$email
Filename: views/add_job.php
Line Number: 10

... and so on and so forth for each $this->validation->property

Note: If you fill in any single field and submit it, they no longer appear.
#4

[eluser]hvalente13[/eluser]
[quote author="awpti" date="1215792094"]You can see it right off the linked site.

A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Validation::$email
Filename: views/add_job.php
Line Number: 10

... and so on and so forth for each $this->validation->property

Note: If you fill in any single field and submit it, they no longer appear.[/quote]

Hi Awpti,

Have you loaded the class in the controller that loads the view, at first time?

Good luck
#5

[eluser]awpti[/eluser]
Was in laughlin for the weekend, slow reply here.

The Validation library is loaded with the config/autoload.php
#6

[eluser]gon[/eluser]
What I do is extending Validation library by putting this code in a file called MY_Library, in application/libraries

Only works for PHP5:

Code:
class MY_Validation extends CI_Validation {    
   function __get($name) {
      return isset($this->$name) ? $this->$name : null;
   }
}


You can then write $this->validation->whatever, and $this->validation->whatever_error even if validation hasn't been run.
#7

[eluser]hvalente13[/eluser]
Can you post the code of the controller that loads the form for the first time?
#8

[eluser]gon[/eluser]
The code is just as usual:

For example, the typical controller function:

Code:
function index() {
   $this->load->library('validation');
   if ($_POST) {
      $this->validation->set_rules( ... );
      if ($this->validation->run() === FALSE) {
          $this->load->view('formsuccess_view');
          return;
      }
   }
   $this->load->view('form_view');
}


And the form view would be just like in the docs examples.
The trick is that by using the library extension I posted, there won't be erros reporting that values hasn't been initialized for the validation object, even if validation hasn't been run.
#9

[eluser]Colin Williams[/eluser]
Very odd. Your code looks fine. And the set_fields() method already does what gon does in his extension:

Code:
foreach($this->_fields as $key => $val)
        {
            $this->$key = ( ! isset($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]);
            
            $error = $key.'_error';
            if ( ! isset($this->$error))
            {
                $this->$error = '';
            }
        }

You can clearly see that $this->validation->field is either '' or the posted value, but never undefined..

I'm at a loss, cause I have very similar code in a 1.6.3 site right now that isn't throwing errors.
#10

[eluser]hvalente13[/eluser]
Hi,

The problem is... if the fields and rules for validation haven't been initialized, then that error occurs

Code:
$rules = array
                (
                        'location'      => 'required|xss_clean',
                        'company'       => 'xss_clean',
                        'email'         => 'required|valid_email',
                        'job_title'     => 'required|xss_clean',
                        'description'   => 'required|xss_clean',
                        'to_apply'      => 'required|xss_clean',
                        'website'       => 'required|prep_url|xss_clean'
                );

                $fields = array
                (
                        'location'      => 'Location',
                        'company'       => 'Company',
                        'email'         => 'E-Mail',
                        'job_title'     => 'Job Title',
                        'description'   => 'Description',
                        'to_apply'      => 'How to Apply',
                        'website'       => 'Website'
                );

That happens because the validation controller it's not the same that the one that loads the form view at first time...

The philosophy of validation class is that it's only needed one controller to initialize and validate the all form and process...

Give it a try!




Theme © iAndrew 2016 - Forum software by © MyBB