Archive for October, 2008

Object Oriented Debate Again…

So the Object Oriented Debate came up at work today… yet again.  I’m a member of a six person team and only a couple of us have any experience with OOP, and then even less with regard to CFOOP (Coldfusion Object Oriented Programming).  The old guard continue to be opposed to OOP breaking into our processes even though it has surely been the trend for Coldfusion since the release of MX. 

I personally resisted OOP at first as I wondered if it really had a place in our section.  We seemed to get by fine with our homegrown “frameworks” (if you can call it that), shared library of includes and user defined functions (written in cfscript) and good old fashioned spaghetti code.  ;-)   However, over time I have felt compelled to take the plunge.  So I’m working on applying OOP and Model-Glue to my next project. 

Model-Glue is an object oriented Coldfusion framework that pretty much requires you to practice OOP in an MVC architecture.  I’ve been using Fusebox for my last couple projects, which does have an OOP implimentation, however, I find Model-Glue a more “native” OOP framework.  Fusebox 5 will support both approaches but I never could wrap my head around the OOP implimentation, possibly because I was use to the old 4.x style. 

Anyway, I’ve decided to take the plunge and that means “sneaking” it into projects here at work.  I’m finding that it isn’t something that yeilds quick returns, but in the long run, code is indeed more managable.  But to get to the point of reaping a return on your investment, you must get beyond the initial learning curve, and also any barriers imposed by your team.  It is really a question of “change management”.  People often don’t like change especially if the benefits aren’t clear or immediate as is the situation with adoption of OOP.

client denied by server configuration

I recently setup a new development server running Apache 2.2.9 and ran into a little problem.  As I configured all my virtual hosts, I found that each time I tried to access anything other than the default root directory, I’d get a forbidden error in my browser accompanied by an entry in my error log:

[Wed Oct 08 11:17:31 2008] [error] [client 127.0.0.1] client denied by server configuration:

Well, since this was something new to me I had to do a little digging and finally found that something new with Apache 2.2 is some added security out of the box.  With defacto settings, Apache now restricts access to all your folders outside of DocumentRoot.  It stems from a change in the default <Directory /> directive in the httpd.conf file:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Satisfy all
</Directory>

To fix the situation, change “Order deny,allow” to “Order Allow,Deny” and change “Deny from all” to “Allow from all”.  In my case, I just want complete and utter access being this is a development server, you might need to restrict by IP segment or other criteria if it is a production server.

Coldfusion and WDDX for Dynamic Form Validation

Every so often, I need to validate a form field from a list that is stored in a database.  Using Coldfusion’s support for WDDX (Web Distributed Data eXchange) works well for this solution.  In my most recent example, I needed to validate a state field, however, the business owners did not want a drop down (the easy way to validate something like this).  Staff using the application wanted to key the data.  Additionally, rules surrounding what is a valid “state” change and can include or exclude various U.S. territories like Guam or the Virgin Islands.  For this reason, I needed to lookup a list of valid states and then loop over those results, comparing to what the staff person keyed into the form.  So lets see how this is done:

The first step is to load the needed libraries for wddx support like so:

<script type=”text/javascript” src=”/CFIDE/scripts/wddx.js”></script>

Next we look up the list of states we want.  This assumes you have a database with at least one column called “code” that holds the two digit abbreviation for states.  Here’s my query:

<cfquery name=”qStatesWorked” datasource=”#systemdsn#”>
select CODE
from state_codes
where state_flag = ‘Y’
and valid_flag = ‘Y’
or code = ‘PR’
or code = ‘VI’
order by code
</cfquery>

In this case, I have a couple of columns that determine if the entry is valid, and if it is an actual state (some are territories).

Now we will load our JavaScript object with the results of our database query:

<script>
<cfwddx action=”cfml2js” input=”#qStatesWorked#” topLevelVariable=”gState”>
</script>

Now, you will need a JavaScript function to loop over the object that contains your query results:

function MatchState(stateCode) {
var rValue = false;
var nRows = gState.getRowCount();
//check that any value in Union Number matches available options from database
// by looping through the union query results
for (x=0; x<nRows; x++)
{
if (stateCode == gState.getField(x,’code’)) {
rValue = true;
}
}
return rValue;
}

This is pretty straight forward, find the number of records in your object with .getRowCount(), loop the object that number of times, performing a value comparison and setting your return value to true if there is a match.

Finally, I need to call out function as part of my master form validation script (of which MatchState() is a part of).  I have a series of “if/else if” checks using the following to check for a valid state code:

else if (!MatchState(form.MAIL_STATE.value)){
alert (“State is incorrect.”);
form.MAIL_STATE.focus();
rvalue = false;
}

If I MatchState returns FALSE, a JavaScript alert is fired, focus is set to the state text box and we set the return value to false.  The form submission is halted and the user knows they have keyed a bad state value.

Try not to get stuck on validating a list of state codes, I know most folks will just lock it down with a static drop down list, but this could be used for any number of things where you need to validate against a list of valid options and you need to allow data to be keyed rather than selected.  I was happy Coldfusion could easily facilitate the use of WDDX as this made the entire process pretty painless.

Microsoft picks JQuery

So I read today that Microsoft is going to adopt JQuery and include it in Visual Studio according to an eWeek article.  This sounds like a big coup for JQuery and I wonder what the folks at Adobe will do with Spry.  My perception has been that Spry hasn’t been hyped very much since it was released.  I never see any announcements about it and versions seem to come out rather slowly.  

I had played with what I thought were the big three AJAX libraries, JQuery, YUI and Spry.  I found Spry to be faster but not as polished as YUI and JQuery I actually found the hardest to use.  I personally believe that with Microsoft (and others) behind JQuery, it will become the dominant AJAX library and the one developers will want to know.  Then again, here I am working with Coldfusion most of the time and it is hardly the most wide spread web scripting language… so maybe it doesn’t even matter.  ;-)