Coldbox Best Practice for Removing Whitespace from elements in RC

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

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 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)


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.

> *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.

> 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.

https://trycf.com/gist/50152bbb7b24d018fea2bd6e39a5489f/lucee5?theme=monokai

> 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.