1. Dynamic submission parameters

There are situations where it's useful to have dynamic parameter names in a form, as opposed to a static list. We'll explain with an example where the user is asked to translate a string into a number of languages. The application then checks if all the translations have been provided.

The languages and correct translations are stored in static arrays in the Java implementation. In a real world case this information would probably be stored and read from a database. Having to update the element definition every time a new language is added would be very tiresome, and to solve this kind of situation RIFE uses dynamic submission parameters.

Example 9.1. Element using dynamic submission parameters

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE element SYSTEM "/dtd/element.dtd">

<element implementation="Dynamic">
  <submission name="translate">
    <param regexp="lang_\w+"/>
  </submission>
</element>      

Take a look at the param tag in Example 9.1, “Element using dynamic submission parameters”. Instead of using the attribute name like we've done so far in the examples, we're using the regexp attribute. This makes the engine match the parameter names against a regular expression, so in the example above any parameter with a name that starts with "lang_" will be a match, for example "lang_English".

Example 9.2. Template used for demonstrating dynamic submission parameters

<html>
  <head>
    <title>Dynamic submission parameters</title>
  </head>
      
  <body>
    <h1>Translate phrase</h1>
        
    The phrase is '<!--V 'phrase'/-->'<br/><br/>
        
    <form action="<!--V 'SUBMISSION:FORM:translate'/-->" method="GET">
      <!--V 'SUBMISSION:PARAMS:translate'/-->
      <table>
        <tr>
          <th>Language:</th><th>Translation:</th>
        </tr>
        <!--V 'langs'/-->
        <!--B 'lang_block'-->
        <tr>
          <td><!--V 'lang'/--></td>
          <td>
            <input type="text" name="<!--V 'name'/-->"/>
          </td>
         </tr>
        <!--/B-->
      </table>
      <input type="submit" name="Submit"/>
    </form>
  </body>
</html>      

Example 9.2, “Template used for demonstrating dynamic submission parameters” shows the template that is used by the Java implementation. The only difference from the templates shown earlier is that the name of the input is a value instead of a hard coded string, to make it possible to set the name from the Java implementation.

Example 9.3. Java implementation of element using dynamic submission parameters

import com.uwyn.rife.engine.Element;
import com.uwyn.rife.template.Template;

public class Dynamic extends Element
{
  private Template mTemplate = null;
  static String[] langs = {"Swedish", "French"};
  static String[] translations = {"God kväll!", "Bon soir!"};
    
  public void processElement ()
  {
    if (!hasSubmission("translate"))
    {
      mTemplate = getHtmlTemplate("dynamic");
      mTemplate.setValue("phrase", "Good evening!");
      Template template = getHtmlTemplate("dynamic");
          
      for (int i = 0; i < langs.length; ++i)
      {
        template.setValue("lang", langs[i]);
        template.setValue("name", "lang_" + langs[i]);
        template.appendBlock("langs", "lang_block");
      }

      print(template);
    }
    else
    {
      print("Translated");
    }
  }
}      

Two parameters are constructed and appended on the template, both with the prefix "lang_", and therefore matching the regular expression used in the element. It would be easy to add more languages, without having to add submission parameters to the element definition.