Using a related item in a custom form

When you are creating a form for your site that submits to a custom item type, the form generate provides all the code you need, as long as you stick to simple field types.

The automatically generated code does not, however, support some field types, such as related items. You can add a related item picker to a custom form, however, by adding the required code.

Here is the code to accomplish this:

<select name="form-name[dymd][field-name][node_ids][]" size="1"> 
  <w:kb:item:each type="related-item-type" sort="name asc">
    <option value="<w:id />"><w:name /></option>
  </w:kb:item:each>
</select>

The trickiest bit here is naming the select correctly. This name must be exactly correct, or the back-end system won't know what to do with the data when it is submitted, and it is a bit complex. Use exactly the name structure show above, substituting in the form-name and field-name for your particular form.

For example, suppose you have a custom item type "volunteers", and this item type has a related item "employee", so you can pick a particular employee when adding a volunteer record. If the form you have generated has the form name "volunteer-form", and the field that is the related item to employees is named "person", then the select element would be:

<select name="volunteer-form[dymd][person][node_ids][]" size="1"> 

Note, in particular, that the text "[dymd]" and "[node_ids][]" is required and must not be modified.

The size attribute can be changed to your liking, just like any HTML select; if it is set to 1, you'll get a pop-up list, and if it is set to any other number you'll get a select box with that many rows.

Now, to populate the options for the select, you just iterate through the items that are of the type to which you are relating. For example, in the example above, you'd use:

<w:kb:item:each type="employee" sort="name asc"> 
    <option value="<w:id />"><w:name /></option>
</w:kb:item:each>

This will create one option element for each employee, with the value set to the database ID and the visible name set to the name field of the item.

Have more questions? Submit a request

2 Comments

  • 0
    Avatar
    Charity Kirk

    Is there a way to use checkboxes rather than a select area?

  • 0
    Avatar
    Charity Kirk

    using checkboxes rather than a select input is straight forward.

    using the above example, the code would be:

    <w:kb:item:each type="employee" sort="name asc"
      <input type="checkbox" name="volunteer-form[dymd][person][node_ids][]" value="<w:id />"><w:name /><br />
    </w:kb:item:each>

Please sign in to leave a comment.
Powered by Zendesk