Skip to main content
Inspiring
September 16, 2024
Answered

One field displays # and other field displays ##

  • September 16, 2024
  • 4 replies
  • 677 views

Team,

 

In my code I'm trying to dynamically replace the # with ##. 

 

Code is ,

for(key in localStruct.AdditionalFieldsToUpdateStruct)  {
                    localStruct.AdditionalFieldsToUpdateStruct[key] = replace(localStruct.AdditionalFieldsToUpdateStruct[key],'##','####','all');
 }
 
But while displaying, one field shows # and other shows ##, 
 

 

Not able to find the reason why this happens,

 

Can you please help?
 
Code where the fields are displayed ,
 
<td class="formRow">
#local.fields[FiName].Display# 
</td>
 
 
 
    This topic has been closed for replies.
    Correct answer Vishnu22410012h6s8

    Hi Vishnu, did that solve the # problem?


    Hi , thank you, we wrapped the display value with Evaluate() function , this resolved the issue. Thanks

    4 replies

    BKBK
    Community Expert
    Community Expert
    September 18, 2024

    @Vishnu22410012h6s8 , Have you solved the problem already? If not, then provide the information asked for and you will get a solution. This problem need not interrupt your work. It is simple to solve.

    Inspiring
    September 23, 2024

    Hi I expect single # to be displayed in UI.  I added the below fix because, if user enters # in the text area field and saves, error page will occur. This could be because coldfusion doesn't allow single #. The below piece of code is in a  repository, say ABC.

     

    for(key in localStruct.AdditionalFieldsToUpdateStruct)  {
                        localStruct.AdditionalFieldsToUpdateStruct[key= replace(localStruct.AdditionalFieldsToUpdateStruct[key],'##','####','all');
     }
     
    The below priece of code saves the value from the ABC repo into the database, gets the saved values from database and dsiplays the values in the UI using below code,
    //get the additional field values
    <cfscript>
        local.GetFieldsForRequest = this.GetFieldsForRequest(arguments.ReId);
    </cfscript>

     
    <cfif local.GetFieldsForRequest.RecordCount>

     
        <cfset local.fields = structNew()>

     
        <cfloop query="local.GetFieldsForRequest">
            <cfset local.fields[FiName] = StructNew()>

     
            <cfset local.configList = TyFiConfiguration>
            <cfset local.fields[FiName] = this.MapConfig(local.configList)>

     
            <cfset local.fields[FiName].value = ReFiVaValue>
            <cfset local.fields[FiName].required = TyFiRequired>

         
               
            <cfif Len(ReFiVaId)>
                <cfset isReadOnly = TyFiReadOnly>
            <cfelse>
                <cfset isReadOnly = false>
            </cfif>

            <cfset local.fields[FiName].readOnly = isReadOnly>

                  <cfsavecontent variable="local.theDisplay"><cfoutput>
                <cfinclude template="Fields\#ReReplace(FiTyName,'[^a-zA-Z]','','all')#.cfm"> <!--- (FiTyName,' ','','all') --->
            </cfoutput></cfsavecontent>

            <cfset local.fields[FiName].Display = local.theDisplay>

        </cfloop>

        
        <cfoutput>
            <div style="text-align:left; overflow: auto; overflow-x: hidden; height:455px;"> <!--- --->
                <table class="table2008" width="50%">
                    <tr>
                        <th class="reverse" colspan="3" style="text-align:center">Additional Information Fields</th>
                    </tr>

                   
                    <cfset FiGroupName = "">
                    <cfset local.previousGroupName = FiGroupName>
                    <cfloop query="local.GetFieldsForRequest">
                        <cfset color = "">
                        <cfif structKeyExists(arguments.FieldErrors,FiName)>
                            <cfset color = "color:red;">
                        </cfif>

                        <cfif ListFindNoCase("Hidden Field",FiTyName)>
                     
                            #local.fields[FiName].Display#

                        <cfelse>

                      
                            <cfif FiGroupName neq local.previousGroupName>
                                <cfif LEN(FiGroupName)>
                                    <tr>
                                        <th colspan="3">#FiGroupName#</th>
                                    </tr>
                                </cfif>
                            </cfif>

                            <tr>
                                <td class="formRow"style="width:1%;#color#">
                                    <cfif local.fields[FiName].required>
                                        *
                                    <cfelse>
                                        &nbsp;
                                    </cfif>
                                </td>
                                <td class="formRow " style="font-weight:bold;#color#" width="30%">
                                    #FiDisplayName#
                                </td>
                                <td class="formRow">
                                    #local.fields[FiName].Display# // Here is where all the field values are displayed
                                </td>
                            </tr>
                            <cfset local.previousGroupName = FiGroupName>

                        </cfif> <!--- <cfif ListFindNoCase("hidden",FiTyName)> --->

                    </cfloop> <!--- <cfloop query="local.GetFieldsForRequest"> --->

                </table>

            </div>
        </cfoutput>
    </cfif>
     
    I also checked the database and can confirm that values of both the fields A and B in database have %23%23(in encoded format which is ##)
     
    Let's assume the value sent to the system for field A (text field)  is "This is Vishnu. My id is #124."
    Similarly the value sent to the system for field B (text area field) is "This is Ragav.My id is #234"
     
    When we check in the UI,
    The field A is displayed as ->   This is Vishnu. My id is ##124.
    The field B is displayd as  ->    This is Ragav.My id is #234
     
    Please let me know if mre information is required. Thanks
    BKBK
    Community Expert
    Community Expert
    September 23, 2024
    quote

    I expect single # to be displayed in UI. 


    By @Vishnu22410012h6s8

     

    Thanks for that explanation. If you expect just a single # in each case, then the following code won't work

     replace(localStruct.AdditionalFieldsToUpdateStruct[key],'##','####','all')

    That is because this code replaces "####" with "##". To illustrate with a different example, the result of

    <cfoutput>#replace("Time flows.","flows","flies")#</cfoutput>

    is: Time flies.


    Suggestion:

    1.  Use chr(35) to represent #.
    2.  Use the regular-expression-replace function, REreplace, to replace any sequence of # characters with a single # character:
      <cfset localStruct.AdditionalFieldsToUpdateStruct={myKey1='myValue1',myKey2='##myValue2',myKey3='####myValue3',myKey4='######myValue4'}>
      
      <cfloop collection="#localStruct.AdditionalFieldsToUpdateStruct#" item="key">
      	<cfset localStruct.AdditionalFieldsToUpdateStruct[key]= REreplace(localStruct.AdditionalFieldsToUpdateStruct[key],'(#chr(35)#){2,}','#chr(35)#','all')>
      
      	<cfoutput>#"Key=" & key & "; " & "localStruct.AdditionalFieldsToUpdateStruct[key]=" & localStruct.AdditionalFieldsToUpdateStruct[key]#</cfoutput> <br>
      </cfloop>​
       The result is:
      Key=MYKEY1; localStruct.AdditionalFieldsToUpdateStruct[key]=myValue1
      Key=MYKEY2; localStruct.AdditionalFieldsToUpdateStruct[key]=#myValue2
      Key=MYKEY3; localStruct.AdditionalFieldsToUpdateStruct[key]=#myValue3
      Key=MYKEY4; localStruct.AdditionalFieldsToUpdateStruct[key]=#myValue4

     

     

     

    BKBK
    Community Expert
    Community Expert
    September 16, 2024

    The displayed ## and # suggest that the end-results were, respectively, the strings '####' and '##'.

    Is that not what you expected? If not, what are the original field names and what did you expect the end-results to be?

    Did you, for example, expect just one # to be displayed in every case? Or the double ## to be displayed in every case?

     

    Also, could you include code that shows how we go from localStruct.AdditionalFieldsToUpdateStruct[key] to local.fields[FiName].Display ?

     

    Charlie Arehart
    Community Expert
    Community Expert
    September 16, 2024

    Separately, I'd argue we can't solve the problem without seeing the values in question. Show us which "key" value works and which does not.

     

    Also, you can often help yourself in this situation by creating a simple test page that does ONLY what's needed to present the problem. That way any of us could run it to confirm we see the same result...but often in creating such a simple test page, one will more easily recognize the problem and solution on their own. 

    /Charlie (troubleshooter, carehart. org)
    Community Expert
    September 16, 2024

    There's an old joke that goes like this: someone goes to a doctor and complains "it hurts when I do this". The doctor replies "don't do this". That's what your question reminds me of. Don't try to escape hash marks in the middle of an expression the way you're doing. You might be able to get it to work, but there's almost certainly an easier way to do what you want.

     

    Dave Watts, Eidolon LLC 

    Dave Watts, Eidolon LLC