Regular Expressions in Field Formats
UI field formatters can be defined that enforce a format according to a regular expression.
The UI field formatters are defined in an app resource at the discipline level. Access the app resources as an admin user by clicking on your user name and selecting Resources then App Resources.
Use the panel on the left of the App Resources page to navigate to the appropriate discipline and click on the Add Resource button. In the dialog asking for a Resource Type, click on App Resource and then on Field Formatters.
Add a new format element to the formats list. This can be done by copying an existing format such as the one above with the name “CatalogNumber”, deleting the autonumber and field elements, and changing the name attribute to a new value such as “CatalogNumberRegex”.
Next add a single field element to the new format with three attributes, type, value, and pattern. The type attribute should be “regex”. The value attribute should be the regular expression to be enforced. The pattern attribute will be used as the mouse-over hint for the field. Finally save the UIFormatters app resource using the button on the bottom right.
This makes the regex formatter available. It must then be assigned to the field in the schema configuration using the name that was given to the new formatter, which is most easily accomplished through Specify 6. After that, the Specify 7 server and any Specify 6 clients should be restarted.
To set the format in the schema configuration:
-
Log into Specify as an admin user.
-
Go to Select → Schema Configuration.
-
Select your language from the Available Locales and click Edit a Schema.
-
In the list of tables, scroll down to collectionobject and click it to select.
-
In the list of fields that appears, select catalogNumber.
-
Expand the drop down list next to the Field Format line to see the different Catalog Number format options.
-
The new format you created in the UI Formatters file should be there. Select it.
-
Click OK.
-
Restart Specify.
-
In the Schema Configuration window, make sure your new UI Formatter is still being correctly assigned to the Collection Object’s Catalog Number field.
Example Implementation
Question
From Ben Richardson at the Western Australia Herbarium:
Is there a way to define a field formatter in Specify v7.9.6.2 that optionally ends with a final letter, e.g. ACC/10001 A, where 10001 is incrementing and the added space and uppercase letter must be unique for the incremented number if it exists?
Answer
In your “Field Formatters” (UIFormatters
) app resource for the collection/discipline, you can configure a format like the one you’ve described here:
<format system="false" name="PostfixAccessionNumber" class="edu.ku.brc.specify.datamodel.Accession" fieldname="accessionNumber">
<field type="constant" size="4" value="ACC/"/>
<field type="numeric" size="5" inc="true"/>
<field type="regex" size="0" value="( [A-Za-z])?"/>
</format>
After this is set, you should see ACC/#####( [A-Za-z])?
shown in the preview for the field. Now you can enter ACC/10001 A
and it will be valid! It will also increment based on your postfix.
To make this look much nicer and be easier for the user, you can update the Accession view definition so that it uses the appropriate default (add default="ACC/#####"
to the field):
<cell type="label" labelfor="1"/>
<cell type="field" id="1" name="accessionNumber" uitype="formattedtext" default="ACC/#####"/>
That’s better!
Now you can create Accession records with an Accession Number that may or may not have a postfix. It also supports incrementing based on the postfix!
Video Demonstration: