KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to handle many web variables
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: October 18, 2021

Here is a method to transform the web variables obtained from a web form into a useful object. The method takes in a collection of web variable names and outputs an object that can be used to get the value of a particular variable. This method should be called after a POST action from a web form using 4DACTION.

// Method: WEB_VARIABLES_TO_OBJECT
// Input: Collection of web variable names
// Output: Object of web variable name & value pairs

var $1 : Collection
var $0 : Object
var $vars : Collection
var $varName : Text
var $idx : Integer
var $val : Text
var $obj : Object

ARRAY TEXT($arrNames; 0)
ARRAY TEXT($arrVals; 0)

$vars:=$1
$obj:=New object

WEB GET VARIABLES($arrNames; $arrVals)

For each ($varName; $vars)
  $idx:=Find in array($arrNames; $varName)
  $val:=$arrVals{$idx}
  $obj[$varName]:=$val
End for each

$0:=$obj

For example, in a sign-up form, a user sends vUsername, vEmail, and vAddress values to 4D via a POST method with /4DACTION/myMethod. In myMethod, WEB_VARIABLES_TO_OBJECT can be used like this:

$varNames:=New collection(“vUsername”,”vEmail"; "vAddress”)
$webVars:=WEB_VARIABLES_TO_OBJECT($varNames)
// check if email already exists in database
$entSel:=ds.Users.query("email = :1"; $webVars.vEmail)

The utility method is especially useful for systematically handling many web variables at once, so that a desired value can be easily retrieved by just calling the variable name as the property of the output object.