Pre-populating form fields with Mura »

4

Tags: Railo, MuraCMS, Coldfusion

Having started using the excellent Mura CMS for various ColdFusion / Railo projects, i am now beginning to understand how to customize and extend the CMS to suit my needs. Once such need required to pre-populate forms that had been build with the Mura form manager when certain url values existed in the query string.

http://www.mydomain.com/index.cfm/my-form/?technology=ColdFusion

If i required that the name field be populated on the page 'My Form', the query string holds the value of 'ColdFusion' for the technology field. The form would look something like this :

Example form

<form name="testForm" action="" method="post">
    <label>Favourite language: </label><input type="text" name="technology" id="technology" />
</form>

 

Now in the contentRenderer.cfc (located in [siteid]/includes/contentRenderer.cfc), the followng function should be added.

dspObjects function in contentRenderer.cfc

<cffunction name="dspObjects" access="public" output="false" returntype="string">
    <cfargument name="columnID" required="yes" type="numeric" default="1">
    <cfargument name="ContentHistID" required="yes" type="string" default="#event.getValue('contentBean').getcontenthistid()#">
    <cfset var rsObjects="">    

    <!--- get the string from super --->
    <cfset var theRegion=trim(super.dspObjects(arguments.columnID,arguments.ContentHistID)) />
    <!--- using the returned string look for any form to pre-polulate --->
    <cfset bodystr = theRegion />
    <cfset bodystrRes = refindnocase(">form.*?(name)*<",bodystr,1,true) />
    
    <!--- ensure a form is present before continuing --->
    <cfif bodystrRes.len[1] gt 0>
        <cfset formstr = mid(bodystr,bodystrRes.pos[1],bodystrRes.len[1]) />
        <cfset formstrRes = refindnocase('(name=\")(\w*)(\")',formstr,1,true) />
        
        <!--- get the name of the form to check for pre-population --->
        <cfset formname = mid(formstr,formstrRes.pos[3],formstrRes.len[3]) />    

        <cfswitch expression="#formname#">    
            <cfcase value="testForm">
                <cfset tech = rereplacenocase(request.technology,"-"," ","ALL") />
                <!--- replace the form field value with the technology in the url --->
                <cfset theRegion = rereplacenocase(theRegion,'name="technology"','name="technology" value="#tech#"',"ALL") />
            </cfcase>            
        </cfswitch>
    </cfif>
    
    <cfreturn trim(theRegion) />
</cffunction>

 

The code above extends the base ContentRenderer and ensures to call the super function dspObject, this ensures that we do not change anything Mura already does, instead before rendering it to HTML we are manipulating the HTML string from the super function to find any values to replace. The starting point to this is the regular expression that looks for the form name, this is imperative to make my method work. After finding the form name and extracting it we run a simple cfswitch statement to check if this particular form requires fields to be pre-populated with values in the request scope. If there are fields in the form that need to be populated, then a simple replace occurs that looks for the field name and insert the value attribute. This is a very simple example and is currently being recoded to be flexible and reusable with minimal of configuration.

In the template that renders the form, something like this is required, this is usual Mura practice to output Content Objects into pages. Instead of calling the dspObjects from  the base cfc, we are running our extended function first and then within in that function, we are executing the base ContentRenderer.cfc function.

Output Mura content object in view

#$.dspObjects(2)#

 

Although there are values hardcoded into the custom dspObjects function, i am in the process of making it more usuable so that form names and form fields can be specified through the cms that need pre-populating and enable more than one field to be pre-populated. This will be a follow up post in the near future and is not to far off.

4 responses to “Pre-populating form fields with Mura”

  1. Matt Levine Says:
    This is a great post to point out how to override default Mura behavior. Another things that I'd like to point out is how you can use the Mura tag inside of the form wysiwyg editor. < input name="example" value="[mura]$.event('technology')[/mura]"> Also, if you want to have really robust custom form interactions you can always use the Mura tag to just include a .cfm file that has the form code in it. [mura]$.dspInclude('display_objects/custom/myCustomForm.cfm')[/mura] Thanks, Matt
  2. jbuda Says:
    @Matt, thanks for posting that. I didn't realise you could just put the [ mura ] tag into the WYSIWYG like that. Guess it makes my code and the idea irrelevant!
  3. Matt Levine Says:
    Not irrelevant at all! It's very important for people to realize that you can override all default rendering behaviors via either the local contentRenderer.cfc and/or eventHandler.cfc.
  4. Tim Says:
    Nice post! Found this very useful, love the way everything is so easy to do with Mura.
leave a reply

Leave this field empty: