Coldbox Best Practice for Removing Whitespace from elements in RC

40 views
Skip to first unread message

ben....@cayuse.com

unread,
May 8, 2018, 2:27:47 PM5/8/18
to ColdBox Platform
Where is the proper location for sanitizing form submission that are being passed into the RC scope?

as an example - I'm trimming some values coming in from a form submission.

ie

<cffunction name="generalSubmit" access="public" returntype="any" output="false">
<cfargument name="Event" type="any" />

<cfset event.setValue('some_field',trim(some_field))>
<cfset event.setValue('some_other_field',trim(some_other_field))>
<cfset var rc = event.getCollection() />

 ....etc

****************

using the event.setValue seems to work before calling getCollection()

.....

Is there a better way to do this?

("this" being trimming white space off form fields being submitted)

Any links to coldBox documentation of best practice are appreciated.



 




Brad Wood

unread,
May 8, 2018, 2:46:32 PM5/8/18
to col...@googlegroups.com
Firstly, you need to call getCollection(), rc and prc and already passed into every handler action.  Secondly, it wouldn't matter when you called it because it's a struct and it's passed by reference.  Thirdly, don't use that code:
 <cfset event.setValue('some_field',trim(some_field))>
because you're relying on "scope hunting" to resolve some_field to url.some_field or form.some_field.  

Finally, if you simply want to trim values at the point of use, just do this:
trim( rc.some_field )
or this 
rc.some_field.trim()
or this
event.getTrimValue( 'some_field' )

If you really want to just trim every single value, then just do this:
rc.each( function( k, v ) {
  rc[ k ] = trim( v );
} );


Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+unsubscribe@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/coldbox/0050019a-e489-492f-9430-b33340fe5d2b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ben....@cayuse.com

unread,
May 8, 2018, 3:54:48 PM5/8/18
to ColdBox Platform

Thanks for the quick reply.

none of those suggested alternatives seem to work.

I'm wanting to remove the white space BEFORE the values are submitted to the DB.
(not after its retrieved from the DB and put into the RC)

<cfset event.setValue('some_field',trim(some_field))> does work - so I'll stick with that until I can get the suggestions to work.

(the code provided just throws a CF error)






To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.
Message has been deleted

ben....@cayuse.com

unread,
May 8, 2018, 4:31:44 PM5/8/18
to ColdBox Platform


On Tuesday, May 8, 2018 at 11:46:32 AM UTC-7, Brad Wood wrote:
Firstly, you need to call getCollection(), rc and prc and already passed into every handler action.  Secondly, it wouldn't matter when you called it because it's a struct and it's passed by reference.  Thirdly, don't use that code:
 <cfset event.setValue('some_field',trim(some_field))>
because you're relying on "scope hunting" to resolve some_field to url.some_field or form.some_field.  

   I guess I'm not clear on why you wouldn't do it that way. (scope hunting)
    It looks like that is how one sets the value within the RC, based on the function name and examples.


Finally, if you simply want to trim values at the point of use, just do this:
trim( rc.some_field )
or this 
rc.some_field.trim()
or this
event.getTrimValue( 'some_field' )

* I ended up using the  statement:

rc.some_field =  event.getTrimValue( 'some_field' );

directly after getting the eventCollection,

That works, but I'm not sure if I'm still "scope hunting" with that method.

--------
 
If you really want to just trim every single value, then just do this:
rc.each( function( k, v ) {
  rc[ k ] = trim( v );
} );

* That code throws an CF error. Is there a working example of how to "process" a form submission with coldbox?
ie a simple example that has a basic FORM submission, where the values are trimmed of whitespace PRIOR to be added to the RC?


    

To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.

Brad Wood

unread,
May 8, 2018, 4:36:58 PM5/8/18
to col...@googlegroups.com
  *maybe so, but it does work. 

Well, you're an adult and can write whatever code you want, but you're breaking encapsulation and bypassing the framework.  Also, you'll make unit testing hard down the road.

> * those all work fine for displaying data coming OUT of the RC. 
>  I want to trim whitespace BETWEEN the FORM submission AND the RC, which is later used to update a DB. 

Sorry, I guess I have no idea what you're doing.  You haven't provided any context or code other than some lines from a handler that show you messing with the request collection.  I don't know what you mean by "between the form submission and the rc".  That doesn't make any sense.  Your browser submits form fields in an HTTP request which present themselves to your CF server in the form scope, which ColdBox takes and merges with the url scope in the request collection for you to use.  If you don't want form fields submitted with whitespace, then don't type the whitespace into the form fields?? I guess I just don't understand.

> outside of coldBox I;'d normally just do 
>  DBfield = '#trim(form.fieldname)#'

Ok, and the ColdBox equivalent of that is just to replace "form" with "rc" and proceed as before.  I suggested that in my first reply.

trim( rc.some_field )

 that code throws a CF error.

What is the error?  What CF version are you on? I didn't test it, but I'm sure you can figure out how to make it work.  That's not even ColdBox-specific code, it's just CFML.  Nothing more than a loop that trims each value in rc and sets it back in.  Rewrite it as necessary until it works, but that's all it's doing.  If you're on some really old version of ColdFusion, the each member function not be supported.


Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


On Tue, May 8, 2018 at 2:59 PM, <ben....@cayuse.com> wrote:


On Tuesday, May 8, 2018 at 11:46:32 AM UTC-7, Brad Wood wrote:
Firstly, you need to call getCollection(), rc and prc and already passed into every handler action.  Secondly, it wouldn't matter when you called it because it's a struct and it's passed by reference.  Thirdly, don't use that code:
 <cfset event.setValue('some_field',trim(some_field))>
because you're relying on "scope hunting" to resolve some_field to url.some_field or form.some_field.  

  *maybe so, but it does work. 
  
 

Finally, if you simply want to trim values at the point of use, just do this:
 
trim( rc.some_field )
or this 
rc.some_field.trim()
or this
event.getTrimValue( 'some_field' )

* those all work fine for displaying data coming OUT of the RC.
I want to trim whitespace BETWEEN the FORM submission AND the RC, which is later used to update a DB. 
outside of coldBox I;'d normally just do 

DBfield = '#trim(form.fieldname)#'


If you really want to just trim every single value, then just do this:
rc.each( function( k, v ) {
  rc[ k ] = trim( v );
} );

* that code throws a CF error.
 

To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.

To post to this group, send email to col...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/coldbox/0050019a-e489-492f-9430-b33340fe5d2b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+unsubscribe@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.

Brad Wood

unread,
May 8, 2018, 4:57:12 PM5/8/18
to col...@googlegroups.com
I guess I'm not clear on why you wouldn't do it that way. (scope hunting)

Well, there's nothing inherently wrong with CF's behavior of scope hunting.  What is frowned upon is using the form or url scopes directly inside of a ColdBox.  The rc is how you access those values in ColdBox.

It looks like that is how one sets the value within the RC, based on the function name and examples.

There's nothing wrong with how you were setting the value back into the rc, I was talking about how you were referencing some_field directly from the form scope.  

rc.some_field =  event.getTrimValue( 'some_field' );

Yes, that will work.  It's one of the options I suggested in my first reply. 

directly after getting the eventCollection,

Again, rc is a struct and passed by reference so it doesn't matter when you do it.  You also don't need to do rc = event.getCollection.  It's in the arguments scope already so long as your on Coldbox 3+

That works, but I'm not sure if I'm still "scope hunting" with that method.

No, not at all.  event.getTrimvalue() acts directly on the internal request collection and rc.some_field is also dealing directly with the rc as a struct.  
Note that rc.foo is the same as event.getValue( 'foo' ).  and trim( rc.foo ) is the same as event.getTrimValue( 'foo ' ).  Those helper methods just act on the internal rc struct.

That code throws an CF error.

Nope, works great for me.  Here's a trycf.com example of that exact snippet running perfectly.  


Is there a working example of how to "process" a form submission with coldbox?

I don't understand what you mean.  You don't need to do anything to "process" a form submission in ColdBox.  The form scope will simply be available via the request collection (rc) which is always the case and you simply use the variables like you normally would.  There's nothing else to it.  If by "process" you mean you want to loop over each of the submitted fields and do something to them (like trim them) then just loop like you normally would in CFML.  rc is just a struct so any flavor of looping mechanism that supports collections will work just fine.  The code I provided does just that.

Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+unsubscribe@googlegroups.com.

To post to this group, send email to col...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages