Welcome Guest, Not a member yet? Register   Sign In
Use the Validation Class with Ajax?
#1

[eluser]mvdg27[/eluser]
Hi,

I'm still experimenting with CodeIgniter, and I'm looking for a way to accomplish the following:

I have the xajax-library running with CodeIgniter and now I want to validate forminputs on blur events. I'm thinking of creating a library called Ajaxvalidator to do this, with a check function like this (concept code):

Code:
function check($field, $check, $value) {
  // does field pass the check?
  
  // yes? add an ok image after the specific field with xajax
  $this->objResponse->Assign($field."_status","innerHTML", '<img src="ok.gif" />');

  // no? add an error image after the specific field with xajax
  $this->objResponse->Assign($field."_status","innerHTML", '<img src="error.gif" />');
  return $this->objResponse;
}

I see that CodeIgniter has a powerfull validation class, so it makes sense to use this functionality instead of rewriting this .. but I don't see how I can access this without really submitting the form. As far as I can see, the function run of the class only looks for POST values.

Could anyone point me in the right direction of how to utilize the Validation class on single fields with Ajax?

Any help would be greatly appreciated!

Thank! -Michiel
#2

[eluser]xwero[/eluser]
I don't use the xajax library so i don't know how you are going to handle the per field validation but you would have to post the field to use the validation library.
Code:
function check($field, $check) {
  // does field pass the check?
  $ci =& get_instance();
  $ci->load->library('validation');
  $rule[$field] = str_replace('-','|',$check);
  $ci->validation->set_rules($rule);
  if($this->validation->run() === true)
  {
  // yes? add an ok image after the specific field with xajax
  $this->objResponse->Assign($field."_status","innerHTML", '<img src="ok.gif" />');
}
else
{
  // no? add an error image after the specific field with xajax
  $this->objResponse->Assign($field."_status","innerHTML", '<img src="error.gif" />');
  }
  return $this->objResponse;

}
The url would be something like http://site.com/index.php/ajax/check/field/check. It's best if you are going to use multiple checks to split them with a hypen because the pipes character could cause problems and if you have callbacks use underscores. an example : trim-required-callback_somefunction.
#3

[eluser]mvdg27[/eluser]
Thanks for your reply xwero. I was actually thinking of using this method to directly call to the xajax-function:

Code:
&lt;input type="text" name="username" onblur="xajax_check(this.name, 'required', this.value)" /&gt;
<span id="username_status" class="status"></span>

As far as I know, this is the only way to invoke xAjax functions. So what I'm actually looking for is a function that replaces $this->validation->run() as that only accepts $_POST. Ideally I would use something like $this->validation->check($value, $check), where value is the value of the field passed by ajax and $check represents the rule in CI-style.

Is there some logic in my approach, or is this typically "not CI style"? Is there anyway to achieve something like this? Maybe writing an extend function for the validation class? I tried reviewing the source code of the validation class, but didn't see how to start ..

Thanks so far, anyway!

-Michiel
#4

[eluser]xwero[/eluser]
Do you know if the ajax function does a post or a get request?

I think you shouldn't worry about the CI way making things work is more important.
As far as your idea goes i think it's best to use a post request because there will be some situations where you must validate more input than a get request can handle.
You are right about the limitation of the validation class to only check the $_POST global. I made a suggestion in the 1.6 beta thread to join the $_POST and $_FILES globals to create a centralized validation. So at the moment you would have to create an alternative run method or overwrite the CI one using and extended validation class.
#5

[eluser]mvdg27[/eluser]
Hi,

I think I got it working now. I use it as a library and I manually fill the $_POST var with the value passed by the javascript function:

Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ajaxvalidation {

    var $CI;
    var $objResponse;

    function Ajaxvalidation() {
        $this->CI =& get_instance();
        $this->objResponse = new xajaxResponse();    
    }

    function check($field, $check, $value) {        

        $rules[$field] = $check;
        $this->CI->validation->set_rules($rules);
        
        $_POST[$field] = $value;
                
        if ($this->CI->validation->run() == FALSE) {
            $this->objResponse->Assign($field."_status","innerHTML", '<img src="'.base_url().'graphics/error.gif');
        }
        else {
            $this->objResponse->Assign($field."_status","innerHTML", '<img src="'.base_url().'graphics/ok.gif');
        }

      return $this->objResponse;
   }
  
}

?&gt;

For my test version this works fine, I might add more stuff to it to make it more flexible, but I think the basic idea is correct .. Any comments are always welcome!

Cheers, Michiel
#6

[eluser]xwero[/eluser]
What is the code in the view file?
#7

[eluser]adamp1[/eluser]
Yes seems the best method I would use is do an ajax request, sending the data in the post variables. Then the controller method you send it to can do the validation and give a result, which your form uses. No need at all to submit the form. Check out post requests using jQuery.

I don't know about this ajax library I my self use jQuery and I know you can send post/get requests to a page and process the output. An ajax library seems a bit odd to me? If you use want to use ajax you need to write javascript, writing the javascript using a php library just seems silly. Is that what the library does? Create the javascript code to perform the action?
#8

[eluser]mvdg27[/eluser]
Hi,

This is the code I would have in my view file

Code:
&lt;input type="text" name="username" onblur="xajax_check(this.name, 'required', this.value)" /&gt;
<span id="username_status" class="status"></span>

Furthermore the general template, outputs the javascript needed by xajax. There the function xajax_check is defined, which corresponds directly to Ajaxvalidator->check(), but that's a typical xajax thing.

Cheer, Michiel
#9

[eluser]xwero[/eluser]
[quote author="adamp1" date="1200592836"]
I don't know about this ajax library I my self use jQuery and I know you can send post/get requests to a page and process the output. An ajax library seems a bit odd to me? If you use want to use ajax you need to write javascript, writing the javascript using a php library just seems silly. Is that what the library does? Create the javascript code to perform the action?[/quote]

The xajax library is a wrapper for the xmlhttprequest object so it only takes care of the ajax portion.
Some developers find it easier to write php code instead of native javascript code and who are we to say they can't do it.

I also like to write my own javascript code. Those javascripts wrappers are too limited for my taste.
#10

[eluser]mvdg27[/eluser]
Correct, the xajax library is a wrapper for the xmlhttprequest object. Since I use Ajax in a limited way and I'm used to it now, I prefer this method.

Anyaway, thanks for the help. I got it working now!

Michiel




Theme © iAndrew 2016 - Forum software by © MyBB