Welcome Guest, Not a member yet? Register   Sign In
Validation error on form input value
#1

[eluser]pieter dekker[/eluser]
I got the following error:
'Undefined property: CI_Validation::$email'

On this line on the view:

Code:
<?=form_input('email',$this->validation->email)?>

This is my controller:

Code:
function index()
    {  
        $rules['email'] = "required|valid_email|callback_checkemail";

        $this->validation->set_rules($rules);
        
        $email = $this->input->post('email');
        $this->data->user->email = $email;
        
        if($this->validation->run()==False)
        {
            $data['view'] = $this->load->view('layout/forgotpassform_view','',true);
        }
        else
        {
            if($this->sendmail($email))
            {
                $data['view'] = $this->load->view('layout/forgotpasssucces_view','',true);
            }
            else
            {
                $data['view'] = $this->load->view('layout/forgotpassfailure_view','',true);
            }
        }
        $this->load->view('forgotpass_loader',$data);
    }


Why is it giving an error? Can someone help me out?
#2

[eluser]gtech[/eluser]
Code:
..
  function index()
  {  
    $rules['email'] = "required|valid_email|callback_checkemail";
    $this->validation->set_rules($rules);

    // May need to add this bit of code.. the 'Email Address' string appears in the error message.
    $fields['email']    = 'Email Address';
    $this->validation->set_fields($fields);
            
    if($this->validation->run()==False)
    {
      $data['view'] = $this->load->view('layout/forgotpassform_view','',true);
    }
    else
    {
      $email = $this->input->post('email');
      $this->data->user->email = $email;

      if($this->sendmail($email))
      {
        $data['view'] = $this->load->view('layout/forgotpasssucces_view','',true);
      }
      else
      {
        $data['view'] = $this->load->view('layout/forgotpassfailure_view','',true);
      }
    }
    $this->load->view('forgotpass_loader',$data);
  }

Have you tried using set fields (its only a hunch)?
#3

[eluser]sherbo[/eluser]
What does your checkemail() function look like?
#4

[eluser]pieter dekker[/eluser]
@gtech: You've solved the problem! Tnx! But why do I need the set fields function?
#5

[eluser]gtech[/eluser]
its because the
Code:
... $this->validation->email
in your view is set up from the set fields array and the standard error message for the rule.

lets use the 'required' rule as an exmple... you can change the standard error message with a more A-Team feel:
Code:
$this->validation->set_message('required', '%s is required you crazy fool');

what happens is when you call the set_fields array with

$fields['email'] = 'Email Address';
$this->validation->set_fields($fields);

you are informing the validation routine what to replace the %s with, when the required rule is not met for email.

in your case 'Email Address', so it will display 'Email Address is required you crazy fool'

$this->validation->email is used to display the error message, if you did not set the fields then it will not know how to display the error message for email.

hopefully that makes sense Smile
#6

[eluser]pieter dekker[/eluser]
Thank you very much, it is all clear to me now!
#7

[eluser]gtech[/eluser]
no probs.
#8

[eluser]Martín M.[/eluser]
Hi.
I'm having the same exact problem (Undefined property: CI_Validation::$url) when trying to re-populate a form.

This is the controller:
Code:
$rules['url'] = "trim|min_length[11]|max_length[128]";
$this->validation->set_rules($rules);

$fields['url'] = "webpage";
$this->validation->set_fields($fields);

if ($this->validation->run() == false) {
    $this->load->view('pdc/add_site');
}

And this is the view:
Code:
<input type="text" id="url" name="url" value="<?php echo $this->validation->url ?>" />

As you can see, setting the fields doesn't help. Is something here that I'm not seeing? Thanks!

Edit: I tried using
Code:
if(isset($_POST['url'])) echo $_POST['url'];
and it works OK, but I'm not sure if this is safe regarding validation. What do you think?
#9

[eluser]tonanbarbarian[/eluser]
I have found that if this is the first visit to the page and therefore no POST data is set then the validation field properties ie. $this->validation->url do not get set.

So what I mostly do in the view is
Code:
<input type="text" id="url" name="url" value="<?php echo isset($this->validation->url) ? $this->validation->url : ''; ?>" />
#10

[eluser]Martín M.[/eluser]
How I couldn't realize before! I get the error only when it's the first load of the form. I like your workaround, tonanbarbarian, thanks!




Theme © iAndrew 2016 - Forum software by © MyBB