Apare todos os espaços em branco de um formulário antes de submetê-lo
Problema:
Retire espaços em branco à esquerda e à direita de todos os campos em um formulário antes de submeter. Pode ser usado em qualquer estrutura ColdFusion cujos elementos são strings.
Solução:
Duplicar a estrutura fonte e percorrê-la aparando cada membro da estrutura.
Explicação detalhada:
<!--- Call the function and pass in the struct you want trimmed --->
<cfset trimmedForm = trimStruct(form)>
<!--- now perform your data insert update utilizing the trimmedForm variable to ensure all your values have been properly trimmed --->
<!--- This function can go at the body of your .cfm page, but I usually include it in a .cfc with other common data manipulation tasks I perform --->
<cffunction name="trimStruct" access="public" returnType="struct">
<cfargument name="structToBeTrimmed" type="struct" required="yes">
<cfset var trimmedStruct = structCopy(arguments.structToBeTrimmed)>
<cfloop collection="#trimmedStruct#" item="key">
<!--- Only trim if the value is a simple datatype; strings, numbers, dates, etc. Otherwise ignore this element --->
<cfif IsSimpleValue(trimmedStruct[key])>
<cfset trimmedStruct[key] = trim(trimmedStruct[key])>
</cfif>
</cfloop>
<cfreturn trimmedStruct>
</cffunction>
Versão em inglês: Adobe ColdFusion Cookbook
Comentários Recentes