Custom Code Field on Forms - Documentation topics on: custom code field on forms,custom fields,.

Custom Code Field on Forms

Custom fields are supported by ContentAPI forms, including the deprecated #submitContent and #submitContentEdit macros.

Custom field are sometimes required when dynamic content selections are desired on the content form. For example, a webmaster may want to provide a dynamically limited list of cities that are located within a certain state that the form contributor has just selected. By using a custom code field, the select list of cities could change as the form contibutor selects a particular state. Use of custom code fields is a powerful way to provide dynamically responsive form fields.

Some javascript handling is required to display values in custom fields on the front end form. The sample code below can be placed in a custom code field named "source", which allows the front end content contributor to choose a host from a multi-select list that is dynamically created from existing hosts in the system:

##Multiselect for hosts where news and events will be pulled from
<select multiple size="10" id="msSource" name="msSource">
  #set($siteList = $dotcontent.pull('+contentType:Host
+live:true +deleted:false',0,'Host.hostName'))
   #foreach ($site in $siteList)
         <option value="$!{site.identifier}"
#if($listTool.contains($source.split(',') ,
"$site.identifier"))selected="selected" #end
>$!{site.hostName}   
</option>
   #end
</select>
<script type="text/javascript">
  function updateSource() {
    var sourceHosts = '';
      var hselect = document.getElementById("msSource");
      for(var i = 0; i    
< hselect.options.length; i++) {
          if(hselect.options[i].selected) {
              sourceHosts = sourceHosts + ',' + hselect.options[i].value;
          }
      }
      document.getElementById("source").value = sourceHosts;
  }
  // Connect the onChange to the select
  dojo.connect(dojo.byId("msSource"), "onchange", null, "updateSource");
</script>