Forum:SMW/SF - how can I preload/default a field to contain the name of the user filling in the (partial) form

Extension:Semantic forms has the

feature however I have learned that this inserts the name of the user who initially created the page being edited with the current form.

I want to be able to automatically insert the name of whichever user is presently interfacing with a (full or partial) form to edit data on a pre-existing page. The form might be the default form, an alternate form or a partial form - it should not matter.

I have experimented with the deprecated Template:USERNAME and the associated UserNameReplace javascript (which uses YUI and not jQuery) however I am not able to make that evaluate and populate a semantic form. Complicating the matter is my desire to treat that input field as hidden. ie. populated but without the user being free to view or edit the username automatically entered into that field.

The nearest workaround I can come up with so far is to tag all user pages with  and then provide a partial form that serves up a dropdown style input box with all names belonging to that category. viz.

It's clumsy and I'd prefer the user not be aware of other user's names while using the form I have in mind for this. najevi 20:24, 18 July 2009 (UTC)

Javascript solution
With thanks to Dantman and Ciencia_al_Poder I now have the following script correctly inserting the current user name into a specific input field of input type text or dropdown but not radiobutton and I have not yet tried listbox.

function preloadThisUserName { $("#bodyContent form.createbox") /* narrows the search */ .find("input[name$='[Thisuser]']") /* affects _any_ template with a parameter/field named: Thisuser */ .val(wgUserName); /* Checks, or selects, all the radio buttons, checkboxes, and select options that match the user name. */ $("#bodyContent form.createbox") .find("select[name$='[Thisuser]']") .val(wgUserName); /* Checks, or selects, all the radio buttons, checkboxes, and select options that match the user name. */ }

$( preloadThisUserName );

The semantic form's template must used the named parameter Thisuser to allow this script to identify the input field by name. Note: wgUserName is case-sensitive. -najevi 09:03, 22 July 2009 (UTC)


 * To add a radio button, this should work:

$("#bodyContent form.createbox") /* narrows the search */ .find("input[name$='[Thisuser]'][value='"+wgUserName+"']:radio").attr("checked","checked");
 * --Ciencia Al Poder (talk) -WikiDex 10:39, 26 July 2009 (UTC)

A demo script at preloadFormfields.js illustrates what I needed to use for each type of input field. Listbox proved the most challenging because the val argument needed to bean array of values.

function preloadDemoUserName { /* OVERWRITE the new user name in the input field for: text, textarea, dropdown & radiobutton */

$("#bodyContent form.createbox fieldset ") .find("input.createboxInput[type='text'][name$='[Demo text]']") .val(wgUserName); /* Set the value attribute of the text input field to be the user name. */

$("#bodyContent form.createbox fieldset ") .find("textarea.createboxInput[name$='[Demo textarea]']") .val(wgUserName); /* Set the value attribute of the textarea input field to be the user name. */

$("#bodyContent form.createbox fieldset ") .find("select.createboxInput[name$='[Demo dropdown]']") .val(wgUserName); /* Highlight any select option that does match the user name. */   $("#bodyContent input:radio[checked][value!='" +wgUserName+ "'][name$='[Demo radiobutton]']") .removeAttr("checked"); /* uncheck any checked radio(button) that does not match the user name */ $("#bodyContent input:radio:not([checked])[value='" +wgUserName+ "'][name$='[Demo radiobutton]']") .attr("checked","checked"); /* check any unchecked radio(button) that does match the user name */

/* ADD the new user name to the input field LIST for: checkboxes & listbox */

$("#bodyContent input:checkbox:not([checked])[value='" +wgUserName+ "'][name*='[Demo checkboxes]']") .attr("checked","checked"); /* check any unchecked checkbox that does match the user name */

var List = $.map($("#bodyContent form.createbox").find("select.createboxInput[name*='[Demo listbox]'] option:selected")                      , function(e) { return $(e).val; }                    ); /* the list of values corresponding to options that have already been selected */ var List = List.concat([wgUserName]); /* add the current user's name to the list */ $("#bodyContent form.createbox ").find("select.createboxInput[name*='[Demo listbox]']").val(newList); }

See also: this note -najevi 01:05, 27 July 2009 (UTC)