Adventures in Model Glue - Redirecting to an anchor link

I needed to redirect users if they failed a captcha test when submitting a form back to that form. Straightforward enough eh? Not so as the form itself appeared at the bottom of a pages content. Which meant when the user was redirected by model glue he came back to the page but saw no form or any message telling him why he had ended up back here.

Outside of Model Glue it would have been straightforward but in the event handler for the captchaTest, as far as i could see, there was no way to add append an anchor to the end of the url of the view so that model glue would load up the view with the form displaying to the user at the bottom of the page. In other words redirect to a view with its url looking like this index.cfm?event=whatever&var=value#anchorName

What i ended doing to get round this is a complete hack, and will probably have Model Glue users covering their eyes in horror.

Depending on whether the captcha passed or not i had to add either a pass or fail result to event handler. This would allow mg to redirect back to the page ok but not at the form. To get this to happen i had to add an argument to view when the captcha failed, and in the view (gasp!) i had to test for that value, and if it was telling that the captacha failed i had to include a piece of javascript forcing the view to reload at the form.

<!---If redirected here because of captcha fail. Then the JSscript allows the page to be reloaded at
the form once again, ready for captcha to be tried again --->

<cfset variables.cp = viewstate.getValue("captchaError")>
<cfif #variables.cp# is "yes">
<script type="text/javascript" language="javascript">
location.hash = "#captchaFail";
location.reload();
</script>
</cfif>
Ugly i know but it works and is all i could come up with. If anybody has any better ways to get round this then please chip in.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
charlie griefer's Gravatar sounds like you want to look into named results.

in your controller, when the captcha test fails, do the following:

<cfset arguments.event.addResult('failure') />

as soon as the controller encounters this, it will go back to the event handler and look for that specific named result. so in the event-handler in your <results> section, you'd have the following:

<result name="failure" do="myFormEventHandler" preserveState="true" />

see the following page in the model-glue docs for more information:
http://docs.model-glue.com/Reference_Materials/Mod...

hope that helps.
# Posted By charlie griefer | 7/18/08 4:56 AM
Paul's Gravatar Cheers Charlie. Man that was fast - i had not even fully finished writing the entry and you had answered. The preservestate is not something i remember seeing. I'll check it out.
# Posted By Paul | 7/18/08 5:03 AM
Raymond Camden's Gravatar Charlie, that isn't the issue here, or not the main issue. MG doesn't provide a way to go to an event AND a particular hash. Forget about form validation. Imagine you have Page A with links to a FAQ (event=faq). If you wanted to link to a particular question on the FAQ, ie:

index.cfm?event=faq#goober

That would work fine in a link (afaik). But now take the result of some form post, as he had. You can't use the <result> attribute to point to an event AND a hash.

I haven't had my coffee yet so I may be off on this.
# Posted By Raymond Camden | 7/18/08 8:11 AM
todd sharp's Gravatar Might be a bit hackish, but I add the anchor in my form action and it works for me on SlideSix.com

Check out the comments form on the slide show view:

http://slidesix.com/index.cfm?event=slideshow.view...

Go ahead and submit the comment form blank - it will anchor back to the comment form so the errors are immediately visible. Here's how my form tag looks:

<form method="post" name="commentForm" id="commentForm" action="/saveComment##comments"...>

As I said, maybe it's hackish - but it works for me...
# Posted By todd sharp | 7/18/08 8:40 AM
todd sharp's Gravatar Ooops...use this link instead please:

http://slidesix.com/view/Integrating-ColdFusion-8-...
# Posted By todd sharp | 7/18/08 8:42 AM
Raymond Camden's Gravatar Todd - but the idea here though (I believe, still no coffee), is to support it with a RESULT being called, and possibly with redirect=true.
# Posted By Raymond Camden | 7/18/08 9:08 AM
todd sharp's Gravatar Well tell Joe to get to work then ;)
# Posted By todd sharp | 7/18/08 9:12 AM
charlie griefer's Gravatar well i completely misread the question. but if ray's going to use the 'no coffee' excuse, i'm gonna use the 'it was 3:00 am' excuse :)

interesting question (now that i'm kind of awake). as far as the "hack" solution... i'll quote mr. camden and say, "have a coke and get over it". not to advocate hacks in and of themselves, but if you've hit a point where the architecture that you're using simply imposes a particular limitation... what else are you supposed to do?

in this case, you are fully aware that in theory, there should be a better way. and maybe there is, but we just haven't thought of it. still ok. point being, you're not implementing a hack because you're unaware that it's a hack... or for the sake of cutting corners to take a particular shortcut... but in this case because a more elegant solution doesn't seem to be available for a specific scenario.

when the elegant solution is unavailable, your only option is to implement the best available solution that you can devise. i normally throw a little comment into the code to remind myself to come back and revisit it at a later date (not that i ever actually do... but the intent was there) :)
# Posted By charlie griefer | 7/18/08 11:50 AM
todd sharp's Gravatar Just a minor thing really, but the use of pound signs in this line is unnecessary:

<cfif #variables.cp# is "yes">

could really just be:

<cfif variables.cp>
# Posted By todd sharp | 7/18/08 1:00 PM
Joe Rinehart's Gravatar Excuse me for being crude but...omfg. I can't believe I missed this in the docs for 2 (I checked the code to make sure it's supported):

<result do="someOtherEvent" redirect="true" anchor="#comments" />

Supporting line of code from rev 2.0.304:

<cflocation url="#stateContainer.getValue("myself")##arguments.event##appendedState##anchor#" addToken="false" />

-Joe
# Posted By Joe Rinehart | 7/22/08 7:09 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner