How to Assign Dynamic Values to Fields in RSForm Pro

Note 1: If you don’t have RSForm Pro (e.g. you only have the free version of RS Form), then the below will not work for you.
Note 2: This post is targeted at intermediate to advanced programmers. A Joomla administrator (unless he’s very technical) will be unable to do the below by himself.

We love our job. Every day we have a new challenge, and every day we learn something new. Today one of our clients wanted us to create a highly dynamic form with RSForm Pro. Of course, you might think that all forms built with RSForm are already highly dynamic, but this one pushed new boundaries. He wanted us to fill in values in input fields, select boxes, radio buttons, checkboxes, and hidden fields dynamically from his database. In other words, let’s say he has a table in the database called jos_vegetables that contains 2 fields: id and name. In his form, our client wants the dropdown box named vegetables to be filled in automatically with values from this table. This will ensure consistency across his website because he won’t have to update the form manually if he elects to add another field to the jos_vegetables table.

Our first attempt was the following:

  • Create a function called getVegetables() which will return a string consisting of the vegetable name and place it in a library file called ourfunctions.php. Here’s the function’s code:

    function getVegetables(){
    	$db =& JFactory::getDBO();
    	$sql = "SELECT name FROM #__vegetables ORDER BY name ASC";
    	$db->setQuery($sql);
    	$arrVegetables = $db->loadResultArray();
    	$strVegetables = "";
    	if (!empty($arrVegetables)){
    		$strVegetables = implode("\n", $arrVegetbales);
    	}
    	return $strVegetables;
    }
    

  • Create the dropdown box called vegetables in the form and give it a value of {rsformvalue getVegetables} in the Items box.

  • Create a system plugin that will run after the System – RSForm! Pro plugin and that will translate all mentions of {rsformvalue [functionname]} with the return value of [functionname].

Technically, the above worked – but we felt that it wasn’t a solid solution to the problem. Creating a plugin to alter the content of a page is usually our last resort. So we thought, could it be that RSForm Pro already has something that will allow us to enter PHP code in the Items box (or in the Default Value field). After some heavy research on the topic, we discovered that RSForm Pro had one undocumented feature: if one adds <code> in the Items box (or, again, in the Default Value field) then all the code in that box will be eval’d. So we tried it, and we entered the following code in the Items box:

<code>return getVegetables();</code>

But we got the following error instead:
Parse error: syntax error, unexpected ‘<' in /administrator/components/com_rsform/helpers/rsform.php(512) : eval()'d code on line 1

So we looked at line 512 in the rsform.php file (located under /administrator/components/com_rsform/helpers/) and we saw the following code:

function isCode($value){
	$RSadapter = RSFormProHelper::getLegacyAdapter();
	if (strpos($value, '<code>') !== false)
		return eval($value);
	return $value;
}

When we looked at the above code, we immediately discovered that it’s buggy and it’ll never work as it should. This is because <code> will be eval’d by the eval function, which will always return a parse error. So we fixed the issue by removing <code> and </code> from $value. Here’s the fixed function:

function isCode($value){
	$RSadapter = RSFormProHelper::getLegacyAdapter();
	if (strpos($value, '<code>') !== false){
		$value = str_replace(array('<code>', '</code>'), '', $value);
		return eval($value);
	}
	return $value;
}

Our code above worked normally when we fixed the isCode function.

As you can see, this is a modification to RSForm’s core file. If you don’t want to fix the bug then there’s a workaround that actually works. The value inside the Items box should be something like the following:

$ignoreThisValue='<code>';return getVegetables();

The above will (cleverly) instruct RSForm that the value must be eval’d but without running into the Parse Error problem because we’re just assigning a dummy variable a value of <code>.

Caveats:

  • Do not encapsulate your code with double quotes (“) or single quotes otherwise you will most likely have a parse error.
  • Make sure to always end your code with a semi-column (;). If you don’t you will have a parse error and the code will not work.

  • Make sure you always use return. PHP’s eval function will not return anything if there is no returned value in the code.

  • PHP’s eval is a very dangerous function. Make sure that the eval’d code (e.g. the code between <code> and </code>) does not include any direct input from the user.

We realize that this post is very advanced, and may be too complicated for many Joomla administrators. But fear not, if you need help adding dynamic values to fields in your RS Form Pro component, then you can always contact us. We will do it for you in no time and at a very affordable price!

3 Responses to “How to Assign Dynamic Values to Fields in RSForm Pro”
  1. Pingback by How to Dynamically Duplicate a Form in RSForm Using PHP | itoctopus — October 18, 2012 @ 2:32 am

    […] our last post on RSForm, we explained how to how to assign dynamic values to fields by using some specially formed PHP code (that code only works in RSForm Pro). In this post, we will […]

  2. Pingback by “Form Component Not Available” Error in RSForm | itoctopus — November 8, 2012 @ 2:39 am

    […] still think that RSForm is a solid component – although this was the second time we were disappointed with this product – we hope it’s the last […]

  3. Comment by RenĂ© — February 15, 2013 @ 9:08 am

    Thanks for this great article. Where should i put the “ourfunctions.php” file?

Leave a comment