Use the Validation Class with Ajax? |
[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) { 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
[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) {
[eluser]mvdg27[/eluser]
Thanks for your reply xwero. I was actually thinking of using this method to directly call to the xajax-function: Code: <input type="text" name="username" onblur="xajax_check(this.name, 'required', this.value)" /> 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
[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.
[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: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 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
[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?
[eluser]mvdg27[/eluser]
Hi, This is the code I would have in my view file Code: <input type="text" name="username" onblur="xajax_check(this.name, 'required', this.value)" /> 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
[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.
[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 |
Welcome Guest, Not a member yet? Register Sign In |