Skip to content

string

A string field is a editable text field with configurable options, including a textarea interface.

Module field definition

javascript
// Configuring the `dogName`and `biography` fields in a module's
// `fields.add` subsection:
dogName: {
  label: 'What is your dog\'s name?',
  type: 'string'
},
// Textarea
biography: {
  label: 'Write a short biography for your dog',
  type: 'string',
  textarea: true,
  max: 800
}

Settings

Required

PropertyTypeDefaultDescription
labelStringn/aSets the visible label for the field in the UI
typeStringn/aSpecifies the field type (string for this type)

Optional

PropertyTypeDefaultDescription
autocompleteStringn/aSets the value of the autocomplete attribute on the field.
defStringn/aThe default value for the field
followingString/Arrayn/aThe name of a field or an array of field names that will be used to automatically generate this field's value. If this field is edited to no longer match the fields it is following, it will stop responding to edits in those fields.
helpStringn/aHelp text for the content editor
hiddenBooleanfalseIf true, the field is hidden
htmlHelpStringn/aHelp text with support for HTML markup
ifObject{}Conditions to meet before the field is active. See the guide for details.
minIntegern/aSets the minimum number of characters allowed
maxIntegern/aSets the maximum number of characters allowed
patternStringn/aAccepts a regular expression string to validate the input. Only values matching the pattern are allowed.
readOnlyBooleanfalseIf true, prevents the user from editing the field value
requiredBooleanfalseIf true, the field is mandatory
requiredIfObject{}Conditions to meet before the field is required. See the guide for details.
sortifyBooleanfalseIf true, creates "sortified" fields. See below.
textareaBooleanfalseIf true, use a textarea interface with multiple lines, which allows line breaks

autocomplete

The string supplied to the autocomplete option is used as the value of the autocomplete attribute for the field, as specified in the HTML standards. This feature suggests possible values based on user inputs and previously entered data, streamlining data entry and improving form usability. This also takes a string of off to disable autocomplete for sensitive fields. For detailed information on how the autocomplete attribute works and the values it accepts, refer to the MDN documentation on autocomplete.

following

This option should be set to the name of a field or an array of field names that will be used to automatically generate this field's value. If this field is edited to no longer match the fields it is following, it will stop responding to edits in those fields.

If an array of fields is passed, the value of each will be concated in the order they are passed in the array.

If this field is nested in an array or object field and is following a field in the parent object, then the name of the field should be prefixed with a <, e.g. following: '<title'. This hoisting also works if the field is following a field in the parent object from a grand-child array or object that is nested within a child array or object using <<. This pattern can be extended for additional levels of nesting.

sortify

Setting sortify: true creates a parallel version of the field that is more intuitive for sorting purposes. This new, additional property's key will be the string field's name, appended with Sortified. Its value will be fully lowercase and have all punctuation removed. Apostrophe will automatically use it if a request is made to sort on the original field.

For instance, if your field's name is lastName and you set sortify: true, lastNameSortified will automatically be created and used when sorting on the lastName field. This provides case-insensitive sorting that also ignores punctuation differences.

NOTE

If you add sortify: true to an existing field, existing objects will get the sortified version of the field:

  • on the next deployment via the apostrophe-migrations:migrate command line task,
  • when the individual Apostrophe documents are saved, or
  • at the next startup when in development.

Migrations like this only need to be run once because on future updates or inserts of a document the sortified property is automatically set.

Use in templates

The Nunjucks nl2br tag can help print textarea strings with line breaks.

nunjucks
<h2>{{ data.piece.dogName }}</h2>
<p>
  {{ data.piece.biography | striptags(true) | escape | nl2br }}
</p>