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.
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>
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.
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.
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...
http://slidesix.com/view/Integrating-ColdFusion-8-...
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) :)
<cfif #variables.cp# is "yes">
could really just be:
<cfif variables.cp>
<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