Dynamic defaults for Semantic Forms

From Semantic MediaWiki Community Wiki
Jump to: navigation, search

Extension: Semantic Forms

Description:

Summary

With a bit of Javascript and some additions to your form definitions you can set the content of form fields to any value you like, constant or generated. This even works, when the field is pre-filled.


Problem

Presently you can set defaults for form fields only to constant values (with some exceptions, granted). It is not possible to set the defaults to dynamic data, e.g. results from template calls or from {{#ask:...}}. If there is preloaded data, you can not force it to default values, at all.


What do you have to do?

  1. Copy the javascript code from User:F.trott/dynamicSFDefaults.js to somewhere where it is executed, e.g. the MediaWiki:Common.js page of your wiki
  2. Include some of the following into your form definition:
<div id="sfdefaults" style="display:none">

 <!-- for input types text, textarea, radiobutton, hidden, submit -->
 <div class="sfdefault" title="templatename[fieldname]" >default value</div>

 <!-- for input types date, datetime, datetime with timezone -->
 <div class="sfdefault" title="templatename[somedatefield][day]" >01</div>
 <div class="sfdefault" title="templatename[somedatefield][month]" >12</div> <!-- give month as a number -->
 <div class="sfdefault" title="templatename[somedatefield][year]" >2345</div>
 <div class="sfdefault" title="templatename[somedatefield][hour]" >12</div>
 <div class="sfdefault" title="templatename[somedatefield][minute]" >34</div>
 <div class="sfdefault" title="templatename[somedatefield][second]" >56</div>
 <div class="sfdefault" title="templatename[somedatefield][ampm24h]" >AM</div>
 <div class="sfdefault" title="templatename[somedatefield][timezone]" >GMT</div>

 <!-- for input type checkbox -->
 <div class="sfdefault" title="templatename[somecheckboxfield]" ></div>

 <! -- for input types checkboxes, listbox -->
 <div class="sfdefault" title="templatename[fieldname]" >name of 1st preselected value</div>
 <div class="sfdefault" title="templatename[fieldname]" >name of 2nd preselected value</div>

</div>


Notes

  1. Tested (well, played with a bit) on IE6 and FF3.5.
  2. You always need the outer div with id="sfdefaults" set.
  3. An id can be used only once in any document, thus only one block of defaults is considered.
  4. The inner divs must have class="sfdefault".
  5. Checkboxes or listbox entries, which are already set, will never be unset by the script.
  6. For text, buttons, textarea and all date input types you can set class="sfdefault preserve" to preserve values that are already set, e.g. from a preload. For date fields the preserve-setting from the day-component is used for all other components (month, year, ...) as well, i.e. there is no use in setting (or not setting) preserve for the month. You can not set preserve for radio buttons, checkbox, checkboxes or listbox.
  7. You can set the default value for "multiple" templates by including "[num]" between the template name and the field name, e.g. <div class="sfdefault" title="templatename[num][fieldname]" >default value</div>. And yes, that is meant literally, you can not replace num by some number. I.e. you can not set different defaults for different incarnations of the multiple template.
  8. This tip works for the standard inputs, too. You can even set the text on buttons this way, if you so desire. Use the fieldnames below.


standard input field name
save wpSave
preview wpPreview
changes wpDiff
summary wpSummary
minor edit wpMinoredit
watch wpWatchthis
cancel That's a link, not an input, sorry.
free text free_text

And if all that div-stuff is too unwieldy for you, then just stick it in a template. That's what this tip is all about, after all. ;-)