Update to jQuery/Kohana Forge validation
I made some updates to the Jquery_Validation library. This library accepts a Forge object and will output the proper jQuery javascript for validation. One bug I solved was that the field names weren’t echoed in the error message.
I also make use of some of the SPL functions of php5 which provide the library with more power. I also added some more error messages so you don’t have to deal with jQuery’s default English messages. The previous release with some examples can be found here. Also shows how to install it.
Files can still be found at the usual places MY_Forge.php, Jquery_Validation.php Both files have changed
Installation and everything is the same.
Now for the SPL magic. I take the example from the earlier release. If you don’t understand what’s happening here I refer you to the earlier post.
public function add_article() { $form = new Forge(); $form->set_attr('id','event')->set_attr('class','ala'); $form->input('title')->label(true)->rules('required|valid_email'); $form->input('text')->label(true)->rules('required'); $form->submit('Send')->set_label(true); if ($form->validate()) { //some stuff } $this->template=new View('template'); $this->template->form=$form->render(); $this->template->form_js=Jquery_Validation::factory($form); }
Now, jQuery validation has quite some stuff my library doesn’t cover. The earlier release allowed you to retrieve the array with rules and messages and you could edit it yourself and pass it as json to jQuery. This can still be done but some other stuff can be done as well.
Say, you want to enable debug in jQuery validation
//continued from above $this->template->form_js['debug']=true;
Ignore some fields when validating
$this->template->form_js['ignore']='.ignore'; //ignore all inputs with class= .ignore
Add ‘valid’ class to validated inputs
Ignore some fields when validating
$this->template->form_js['success']='valid';
Add new rules and messages
$this->template->form_js['rules']['title']['remote']='check_title.php'; //ajax request to the file to check the title for correctness $this->template->form_js['messages']['title']['remote']='Title is already in use';
Well, that’s easy, isn’t it.
Now if you want to echo the form
echo $this->template->form_js;
Does this work? Yes, this works. It will output the jQuery validation stuff for your Forge forms + whatever extras you need.
What’s still missing? Nothing much I think, I might want to extend the library to also allow input from the new Validation library in Kohana. Also not all Kohana rules match with jQuery rules, I might solve this by implementing addMethod in the library. Rules in Kohana missing in jQuery might then dynamically be added to jQuery.
So the valid_alpha rule in Kohana is missing in jQuery. Using addMethod I could dynamically add it to jQuery validation whenever you use the valid_alpha rule.
April 18th, 2008 at 2:05 am
Congratulations on your blog FULL of very usefull info on Kohana.
I am specially interested in this jQuery addon for Forge. But I am having a problem with the Jquery_Validation.php file. The link (below) is not working. Where can I download this file from?
http://kohana-mptt.googlecode.com/svn/trunk/Jquery_Validation.php
Thanks in advance,
Art
April 18th, 2008 at 9:59 am
http://kohana-mptt.googlecode.com/svn/trunk/Forge_Jquery_Validation.php
April 18th, 2008 at 11:33 pm
Thanks a lot dlib!
June 10th, 2008 at 4:29 pm
Hi dlib,
Thank You for very usefull library!
Please tell me where should i put ‘check_title.php’ to be visible for this example?
I tried also to set other link for example: $this->template->form_js['rules']['title']['remote']=’/project/mycontroller/check_title/’ ;
but it doesn’t work..
Any suggestion?
Thanks,
Dominik
June 10th, 2008 at 6:18 pm
I haven’t actually tested this so I don’t exactly know. Try it with an absolute url, or first try to do it in js and then check for what works.