22

Dec

jQuery Validation with multiple (mostly) identical forms

December 22nd 2009 4:16 pm by Matt

I came across an issue today with jQuery with multiple forms. I’ve got a page that’s dynamically generating possibly several dozen mostly identical forms on a single page. “Why would you do that?” you might ask. Well, imagine a data set where I want to be able to edit each row independently and you’ve got an idea of what I’m doing. I know it’s not pretty, but it’s definitely an easy and straight forward way to do this. Here’s an example of a typical form that I might have 15 of in this script:

1
2
3
4
5
6
7
8
9
<form action="/form/process" method="post" class="question_form">
	<label for="questiont">Question:</label>
	<input class="required" type="text" name="question" />
	<br />
	<label for="answer">Answer:</label>
	<input class="required" type="text" name="answer" />
	<br />
	<input type="submit" value="Submit!" />
</form>

So, you’d think the logical thing to do for quickly running validation on all these forms would be:

1
2
3
$(document).ready(function() {
	$(".question_form").validate();
});

Unfortunately, because the form fields shared the same name, the validation plugin hiccuped with this solution. Now I’m using custom error classes and rules in my actual implementation, so maybe this problem is more specific and isn’t reproduced quite this easily – but basically what happens is that if you have 10 “Question” forms, the validation plugin will see the “Question” field on the first form and call it good. Obviously, we can’t have that, we want each form validating separately.

The solution? Use the each selector to iterate through the forms, of course:

1
2
3
4
5
$(document).ready(function() {
	$(".question_form").each(function() {
		$(this).validate();
	});
});

voila! Easy, but I want to remember it for later :)

02

Jun

Using Javascript to calculate Dates

June 2nd 2009 1:44 pm by Matt

This is also a part of this obnoxious and ridiculous reporting application that I’m using. I have a need to populate start date and end fields with a more user-friendly system. So I’ve got a dropdown with a few set options (Today, Last Week, Week to Date, etc.). Most of the code for these things is available around the internet, but the tricky part was calculating the weekly dates relative to the current date. IE grabbing “Week to date” and “Last Week” date ranges.

Read the rest of this entry »

28

May

Restoring Checkbox Selections from a Database

May 28th 2009 5:04 pm by Matt

In a project I’m working on right now I’m making a dynamic reporting application. Basically I’m trying to take what I can do easily with a few lines of SQL and turn it into a usable interface for people who don’t know what they’re doing. It has summary reporting (group by), filters (wheres), etc. As you can imagine, this system has a plethora of fields that can be selected to be included in the report. No big deal, they’re all dynamically generated anyway. The catch is when I want to save a report, effectively restoring selections from a database.

Read the rest of this entry »