Welcome Guest, Not a member yet? Register   Sign In
validation_errors() returning an empty string after upgrading to 2.1.0
#1

[eluser]mcryan[/eluser]
I've tried this on 2 instances of CI 2.1.0, one downloaded from the website, the other pulled from the stable branch on GitHub, both return an empty string when form validation fails.

Initially I noticed this on my own forms, then used the form validation example in the user guides and the outcome is the smae: <?php echo validation_errors(); ?> does not output anything.

I have tried this on PHP 5.2 and 5.3 on different servers, but the same problem is happening.

Any advice?
#2

[eluser]skunkbad[/eluser]
[quote author="mcryan" date="1329694016"]I've tried this on 2 instances of CI 2.1.0, one downloaded from the website, the other pulled from the stable branch on GitHub, both return an empty string when form validation fails.

Initially I noticed this on my own forms, then used the form validation example in the user guides and the outcome is the smae: <?php echo validation_errors(); ?> does not output anything.

I have tried this on PHP 5.2 and 5.3 on different servers, but the same problem is happening.

Any advice?[/quote]

I am using it just fine. There must be something else wrong in your code. Maybe shoe some code so we can see whats wrong.
#3

[eluser]mcryan[/eluser]
I started from a clean installation of CI 2.1.0 created a new controller and views and it worked fine. I think one of my config files or something else in the application directory must be broken. I used identical code on the new instance that worked and the existing instance and it failed again, so it has to be that.

I'll update here once I crack this one. Will give it another go later on.
#4

[eluser]mcryan[/eluser]
The problem was that I was extending form_helper.php and had to make a few changes in my version of form_helper in order to line up with the changes in 2.1.0.

Glad I finally sorted this one out!
#5

[eluser]ToddyBoy[/eluser]
I was hoping this would solve my problem as well, but it didn't.

I'm getting the validation errors appearing in my view before the validation is being called.

Here's my controller:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! class_exists('Controller'))
{
class Controller extends CI_Controller {}
}

class Entrar extends Controller {

function __construct()
{
  parent::__construct();
  $this->load->library('file_upload');
  $this->load->library('session');
  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');
}

public function test()
{
  // Validate form input
  $this->form_validation->set_rules('username', 'Username', 'trim|required');
  $this->form_validation->set_rules('password', 'Password', 'trim|required');

  if ($this->form_validation->run() == true){
   // If the validation is successful
   $this->session->set_flashdata('message', 'Validation successful');
   redirect('entrar', 'refresh');
  } else {
   // Display the form
   // Set the flash data error message if there is one
   $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

   $this->data['username'] = array('name' => 'username',
    'id' => 'username',
    'type' => 'text',
    'value' => $this->form_validation->set_value('username'),
   );
   $this->data['password'] = array('name' => 'password',
    'id' => 'password',
    'type' => 'password',
   );

   $this->load->view('media/test', $this->data);
  }

}

Here's my view:
Code:
<!DOCTYPE HTML>
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;title&gt;&lt;/title>

&lt;meta name="Description" content="" /&gt;
&lt;meta name="Keywords" content="" /&gt;

&lt;/head&gt;

&lt;body&gt;
  
&lt;?php if ($message) { if($message == "<p>Validation successful</p>"){ ?&gt;
<div id="success">&lt;?php echo $message;?&gt;</div>
&lt;?php } else { ?&gt;
<div id="error">&lt;?php echo $message;?&gt;</div>
&lt;?php } } ?&gt;
   &lt;?php echo form_open("entrar");?&gt;
    <fieldset>
     <label for="username">Username</label>
     &lt;?php echo form_input($username);?&gt;
     <br>
     <label for="password">Password</label>
     &lt;?php echo form_input($password);?&gt;
     <br>
     <label for="remember">Remember Me</label>
     &lt;?php echo form_checkbox('remember', '1', FALSE, 'id="remember"');?&gt;
     <br>
     &lt;?php echo form_submit('submit', 'Login', 'id="submit"');?&gt;
    </fieldset>
   &lt;?php echo form_close();?&gt;

&lt;/body&gt;
&lt;/html&gt;

Here's the rendered html when the page loads with the error messages appearing:
Code:
<!DOCTYPE HTML>
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;title&gt;&lt;/title>

&lt;meta name="Description" content="" /&gt;
&lt;meta name="Keywords" content="" /&gt;

&lt;/head&gt;

&lt;body&gt;
  
<div id="error"><p>The Username field is required.</p>
<p>The Password field is required.</p>
</div>
   &lt;form action="https://mysite.com/entrar" method="post" accept-charset="utf-8"&gt;    <fieldset>
     <label for="username">Username</label>
     &lt;input type="text" name="username" value="" id="username"  /&gt;     <br>
     <label for="password">Password</label>
     &lt;input type="password" name="password" value="" id="password"  /&gt;     <br>
     <label for="remember">Remember Me</label>
     &lt;input type="checkbox" name="remember" value="1" id="remember" /&gt;     <br>
     &lt;input type="submit" name="submit" value="Login" id="submit" /&gt;    </fieldset>
   &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

I've used this code on another installation and it works fine. The other installation I KNOW is 2.1.0. This current installation I THINK is 2.0.2.

I downloaded a 2.1.0 installation from github and copied the form_validation helper from 2.1.0 into the not working version and I didn't get anything rendered at all. So I backed out that change and am back to square one.

When I actually go through the form and enter a username, for example, and click submit, I only get the error for the password being required. Entering data into both fields, I get the successful validation message.
#6

[eluser]mcryan[/eluser]
You can't just use a single 2.1.0 file with a 2.0.2 installation. Follow the upgrade instructions. Essentially you replace your entire system folder and you'll have to add a few config files. The only reason I was having a problem is because I extended a helper and needed to make some changes in there.

Besides that, I don't really understand what problem you are actually having.

Quote:When I actually go through the form and enter a username, for example, and click submit, I only get the error for the password being required. Entering data into both fields, I get the successful validation message.

This sounds perfect, what's the problem?
#7

[eluser]InsiteFX[/eluser]
Because you are assigning the validation errors to a variable!

Code:
// in view should be
&lt;?php echo validation_errors(); ?&gt;
#8

[eluser]Chathuranga Tennakoon[/eluser]
Hello ToddyBoy

i also believe that you cannot assign the error message values for the variables. therefore the below line is not required in your controller.
Code:
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

just remove that line and put
Code:
&lt;?php echo validation_errors(); ?&gt;
as InsiteFX has said in your view.




Theme © iAndrew 2016 - Forum software by © MyBB