• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation error message is missing the field name

#1
[eluser]SouthOfZeeland[/eluser]
Howdy!

I have a problem with the validation error message. When I submit a form, and a required field is missing, the error message should read "Title is a required field". But the error message I get is "is a required field".

I have made sure that my input-field has the same 'name' as my $rules['name'] and $fields['name']. The standard error message is "%s is a required field".

Any idea what I'm doing wrong?

TIA

#2
[eluser]gtech[/eluser]
Hello its very difficult to see without your code whats going on.. I tried following the example from the validation documentation and the %s worked fine for me, maybee you could cut and copy the code examples, especially the controller in "Callbacks: Your own Validation Functions". Then you can satisfy yourself it works.


the following code also worked for me (slightly modified from documentation)
Code:
function index()
    {
        $this->load->helper(array('form', 'url'));    
        $this->load->library('validation');
            
                $this->validation->set_message('required', '%s custom message here');

        $rules['password']    = "required";
        $rules['passconf']    = "required";
        
        $this->validation->set_rules($rules);
            
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }

views/myform.php
Code:
<html>
  <head>
    <title>My Form</title>
  </head>
  <body>
    <?=$this->validation->error_string; ?>
    <?=form_open('form'); ?>
      <h5>Password</h5>
      &lt;input type="text" name="password" value="" size="50" /&gt;
      <h5>Password Confirm</h5>
      &lt;input type="text" name="passconf" value="" size="50" /&gt;
      <div>&lt;input type="submit" value="Submit" /&gt;</div>
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;

resulting in
Code:
password custom message here
passconf custom message here

#3
[eluser]SouthOfZeeland[/eluser]
Controller:
Code:
$rules["title"] = "required|max_length[255]";
$this->validation->set_rules($rules);

$fields["title"] = $this->input->post("title");
$this->validation->set_fields($fields);

if ($this->validation->run() == FALSE) {
   //load view and so on
}

View:
Code:
&lt;?php echo $this->validation->error_string; ?&gt;
            
<p><label for="title">Title</label>
&lt;input type="text" name="title" id="title" size="100" value="&lt;?php echo $this-&gt;validation->title; ?&gt;" /></p>

That's my code. The value in the lang-file is
Code:
$lang['required'] = "The %s field is required.";

#4
[eluser]gtech[/eluser]
humm I see what you mean.. the set_fields seems to be overwriting the set_rules, if you take the set_fields out it works.. let me have a play with the code..

#5
[eluser]gtech[/eluser]
OK I have an answer but not sure you will like it :-(

When using set fields the $fields["title"] bit has the text string of what will appear in the error message (it will be substituted in the %s bit of your default error message). The value that appears in your textinput box will be your the post result NOT what you set $fields["title"] to.

Hope that helps :-) it confused me.

Code:
&lt;?php

class Form extends Controller {
  function index()
  {
    $this->load->helper(array('form', 'url'));
    $this->load->library('validation');

    $rules["title"] = "required|max_length[2]";
    $this->validation->set_rules($rules);

    $fields["title"] = "FILED NAME TO DISPLAY IN ERROR MESSAGE GOES HERE";
    $this->validation->set_fields($fields);

    if ($this->validation->run() == FALSE) {
       $this->load->view('myform');
    }
  }
}
?&gt;

#6
[eluser]SouthOfZeeland[/eluser]
Ah, now I see! It works now!

Thanks a bundle!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.