Adding jQuery form validation - HOW TO

JP

Member
This HOW TO is for DaDaBIK 4.2

What you will need:

The jQuery validation plugin from this site.
└► http://bassistance.de/jquery-plugins/jquery-plugin-validation/

It requires jQuery 1.3.2 or 1.4.2 which can be downloaded from here.
└► http://jquery.com/

(Default download option for production is all you need.)

Open your header.php file and add the following in between your <head> </head> tags

<script language="javascript" type="text/javascript" src="PATH TO JS FOLDER/jquery-1.3.2.min.js"></script>
<script language="javascript" type="text/javascript" src="PATH TO JS FOLDER/jquery.validate.js"></script>


jQuery can be called from class= for each form element but I didn't want to hard code all that into business_logic.php so all the jQuery form rules are in a separate js file.

<script language="javascript" type="text/javascript" src="PATH TO JS FOLDER/jquery.form.rules.js"></script>

The form does need an id= which DaDaBIK already has in the function build_form section of business_logic.php (line 491)

Example (line 515): $form .= "<form id=\"dadabik_main_form\" name=\"contacts_form\" method=\"post\" action=\"".$action."?table_name=".urlencode($table_name)."&function=".$function;

But since this is used in all forms (insert, update, search etc..) you could run into trouble if you had the same form elements in an "update" form and a "search" form and validation was only needed for the "update" form. So what I did was add IF statements for the different form types to change the id= as needed.

Example for form type update:

(This replaces line 515 above.)

if ( $form_type == "update" or $form_type == "ext_update") {
$form .= "<form id=\"update\" name=\"contacts_form\" method=\"post\" action=\"".$action."?table_name=".urlencode($table_name)."&function=".$function;
}

You can add IF statements for the other forms types (insert, search etc.) below this one to suit your needs.

Below is an example of the form rules used in the update form from my demo site. This would be placed inside the jquery.form.rules.js file.

$(document).ready(function() {
$("#update").validate({
rules: {
date_received: {
required: true,
date: true
},
received_by: {
required: true
}
},
messages: {
date_received: {
required: "Date received is required",
date: "Please enter a valid date."
},
received_by: {
required: "Received by is required"
}
}

});
})


Note: The date_received and received_by are element names in my form/database and are just an example.

You can add some style to your validation too. Below is what I added to my CSS style sheet.

label.error { float: none; color: red; padding-left: 0.5em; vertical-align: middle; font-size: 0.875em; font-style: italic;}

The element label and class error are the default values in jquery.validate.js and are dynamically created by the script, no need to add anything to your form.

Below is jQuery form validation in action when the form is saved without filling in all the required fields:

validate1.png


And here is an example of one of the form fields with partial information. jQuery form validation catches this and gives you the appropriate message.

validate2.png


Hope you find this useful / helpful.


My DaDaBIK Demo: http://dadabik.kicks-ass.net

 

ammar

New member
Dear JP,

I have followed all your steps but data validation while inserting a new record is not working.
I think it is due to non-availability of "id" attribute of input fields.
With "IF" we have changed the id of form only but what about other items?
How we can get the values of input fields in jquery variables to further manipulate them.

I am new to jQuery, would appreciate if you can describe these steps briefly.
I am using
XAMPP for Windows Version 1.7.2
MySQL 5.1.37
PHP 5.3.5
DaDaBIK 4.2

Ammar.
 

JP

Member
Ammar,

Do you have a working site I could look at? I'm not sure I know just yet what you may have missed.
 

meanster99

Well-known member
Hi JP,

I have followed your instructions for this jquery validation mod and it works fine on the 'update' forms but not for the 'insert' forms (just as Ammar above reported). I added an extra couple of IF statements as per your instructions, as shown below:


$form = "";
if ( $form_type == "update" or $form_type == "ext_update") {
$form .= "<form id=\"update\" name=\"contacts_form\" method=\"post\" action=\"".$action."?table_name=".urlencode($table_name)."&function=".$function;
}

if ( $form_type == "insert") {
$form .= "<form id=\"insert\" name=\"contacts_form\" method=\"post\" action=\"".$action."?table_name=".urlencode($table_name)."&function=".$function;
}

if ( $form_type == "search") {
$form .= "<form id=\"dadabik_main_form\" name=\"contacts_form\" method=\"post\" action=\"".$action."?table_name=".urlencode($table_name)."&function=".$function;
}

I only need the validation to work on the update and insert forms. What am I doing wrong?

Thanks in advance,

Matt
 

meanster99

Well-known member
Hi JP,

Think I know what's wrong - haven't added the 'insert' validate to the jquery.form.rules.js. How would I add it to the following:

$(document).ready(function() {
$("#update").validate({
rules: {

Maybe replace the bold line above with:

$("#update","#insert").validate({

?? I will have a play and report back if I get it working!!
 

meanster99

Well-known member
OK, I've managed to get it to work - not sure if it's most efficient method but I just copied the contents of the jquery.form.rules.js and pasted at to the bottom of that same file and changed the '#update' part to '#insert', and all appears to be working!
 

JP

Member
Matt,

Sorry I missed your posts, haven't been on much. Glad you got it to work. I can say this, jquery rules have to be exact. If not jquery in your form will not work at all.
 

meanster99

Well-known member
No problem JP - at least it gets me thinking when I have to work things out for myself! Perhaps you could help with the validation of dates though? I have dates in dadabik set to literal_english in config.php but use your datepicker mod for entering dates. All worked fine. However, now the jquery validation for date shows an error unless I enter the date as literal_english (i.e. 6 feb 2012 etc), which is not ideal (and defeats the purpose of the datepicker mod!).

My workaround has been just to remove the validation for dates from the jquery.rules file but ideally I would like the validation for dates. I looked at the validation jquery code but couldn't even find the date format part. Would this be an easy fix without changing the dadabik date format from what it is currently?

Thanks,

Matt
 

JP

Member
I have to admit it has been a while since I messed with any of this code. I do know that my config.php is literal_english as well and jquery works fine on my datepicker field. Let me think about this and or look at my existing code to see how I have this setup and I'll get back to you.
 

JP

Member
Ok, after looking at my install, I need to ask you something, are you sure it is a jquery error and not a Dadabik/SQL error? Reason I ask is I think I know why you are getting the error. my date_received field (Date Received) is not set as a date or datetime type in mysql, it is set as char(20). I think I did this to avoid the problem you are having. I use jquery to force the input to be a date and that seems to work. Since it is not a date or datetime then the literal english for this field only is a non issue.
 

hedo

New member
oh thx for tutorial.

-----------------------------------------------------------------------------------------------
mocne kartony.
 
Top