CodeIgniter Forums
Can't get the Form Validation library to work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Can't get the Form Validation library to work (/thread-25521.html)



Can't get the Form Validation library to work - El Forum - 12-15-2009

[eluser]AlexJ[/eluser]
Hello,

My Form Validation works (it returns false if a field is not OK, and it returns true when all the fields are correct), but it doesnt show any validation errors, I traced it down to the following bit of code:

Code:
function &_get_validation_object()
  {
      $CI =& get_instance();

      // We set this as a variable since we’re returning by reference
      $return = FALSE;

      if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes[‘form_validation’]))
      {
        return $return;
      }

      $object = $CI->load->_ci_classes[‘form_validation’];

          // $object = “form_validation”
          // $CI->$object doesn’t exist: Undefined property: Webshop::$form_validation
      if ( ! isset($CI->$object) OR ! is_object($CI->$object))
      {
                // returns false, so an empty validation_errors() occurs even when some of the rules arent satisfied.
        return $return;
      }

      return $CI->$object;
  }

One little note about the Webshop controller, it extends a custom Controller named V_Controller, which extends the native controller and adds some methods.

I am not really familiar with the inner working of CodeIgniter so I have no idea how to fix this.

Anyone?

Edit: I think it is because the validation_errors is called from a view file, for some reason my libraries are not available in view files (maybe because I use a extending controller?)

This occurs even when I autoload the libraries.


Can't get the Form Validation library to work - El Forum - 12-15-2009

[eluser]runrun[/eluser]
After loading the validation class and setting rule (you sure did this) You should have this in your controller

Code:
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');

And you should have this in your view file in order to display respective errors to respective form fields.
Code:
&lt;input type="text" name="username"/&gt;
<span>&lt;?=form_error('username')</span>;
&lt;input type="text" name="email" /&gt;
<span>&lt;?=form_error('email')?&gt;</span>



Can't get the Form Validation library to work - El Forum - 12-15-2009

[eluser]AlexJ[/eluser]
Thanks for your reply, aren't delimeters default '<div>', and form_error() also returns an empty string (because the &_get_validation_object() also returns false in that case).

Im currently working on a dirty solution, I pass my view files the error message because it is available in my Controller.


Can't get the Form Validation library to work - El Forum - 12-15-2009

[eluser]Flemming[/eluser]
Hey Alex,

I don't mean any disrespect but I've been using the form validation library for a long time and there's nothing wrong with it. If you're finding you need to resort to a 'dirty solution' then perhaps you should take a few mins to just re-read the user guide and follow the examples there to the letter. I'm pretty sure you will find it all works perfectly! :-)


Can't get the Form Validation library to work - El Forum - 12-15-2009

[eluser]brianw1975[/eluser]
[quote author="Flemming" date="1260907914"]Hey Alex,

I don't mean any disrespect but I've been using the form validation library for a long time and there's nothing wrong with it. If you're finding you need to resort to a 'dirty solution' then perhaps you should take a few mins to just re-read the user guide and follow the examples there to the letter. I'm pretty sure you will find it all works perfectly! :-)[/quote]

And make sure you read the right one! (this killed me for an hour or two a long time ago (before they put in the note of being newer), so I know how you feel.

Form validation user guide