File attachment fields: restrict file types
Many companies use file attachment fields to store things like photos of their products or customer contracts. Wouldn’t it be nice to control what types of files users are allowed to upload? Read on to learn how you can use custom data rules to restrict which types of files are can be uploaded.
What are custom data rules?
Custom Data Rules allow app builders to create business logic that can be applied anytime someone adds or updates data in a Quick Base table. Whether a user is updating a few rows in Grid Edit or uploading new records from a spreadsheet, adding a Custom Data Rule allows administrators to automatically validate changes to data and ensure that everything is in the right format.
To create custom data rules, you write a conditional formula which lists all the types of invalid data you want to disallow and a matching error message for each type of invalid data. To get started, open the table where you want to add custom data rules, then click the blue gear icon. Next, choose Advanced Settings and check the box labeled “Turn custom data rules on?”. Finally, enter your formula and then click Save at the top of the page.
NOTE: You could use form rules for this as well, but it’s now a best practice to avoid using form rules for data validation. The reason is, form rules only apply when users enter data one record at a time using a form. Custom data rules, on the other hand, are comprehensive. They apply anywhere data could be entered, like grid edit, via API calls, imports, etc. Form rules are still helpful for displaying fields/sections conditionally, but you should no longer use them for validating data.
Learn more about custom data rules here.
Example custom data rules for restricting field types
Enter formulas like the ones below as a custom data rule for your table, so you can restrict what types of files may be uploaded in a specific field.
|If you want to...
||Use this formula
|Restrict a contract field to only accept PDFs.
var Text fileType = Right(ToText([Customer Contract]),".");
If($fileType != "pdf",
"Can only upload PDF files to the Customer Contract field.")
|Restrict a photo field to only accept JPGs, JPEGs, and PNGs.
var Text fileType = Right(ToText([Photo]),".");
If($fileType != "png" and $fileType != "jpg" and $fileType != "jpeg",
"Can only upload JPG, JPEG, or PNG files to the Photo field.")
|Restrict any attachment field so it does not accept files of potentially unsafe types such as EXE or JS.
var Text fileType = Right(ToText([Attachment]),".");
If($fileType = "exe" or $fileType = "js",
"Cannot upload unsafe types of files like EXE or JS.")
Each example above restricts what type of file may be uploaded for a single file attachment field. If your table includes several file attachments field which should restrict file type, you can expand your conditional formula to cover that. For instance, an HR on-boarding app may have fields for a resume, W9 form, and background check. If you want to ensure all files upload to all three of those fields are PDFs, your formula will look like this:var Text fileTypeResume = Right(ToText([Resume]),".");var Text fileTypeWNine = Right(ToText([W9]),".");var Text fileTypeBackgroundCheck = Right(ToText([Background Check]),".");var Text checkResume = If($fileTypeResume = "exe" or $fileTypeResume = "js","Resume: Cannot upload unsafe types of files like EXE or JS.");var Text checkINine = If($fileTypeWNine = "exe" or $fileTypeWNine = "js","W9: Cannot upload unsafe types of files like EXE or JS.");var Text checkBackgroundCheck = If($fileTypeBackgroundCheck = "exe" or $fileTypeBackgroundCheck = "js","Background Check: Cannot upload unsafe types of files like EXE or JS.");List(" ", $checkResume, $checkINine, $checkBackgroundCheck)
When writing your error messages, be specific. For instance, if your custom data rules cover multiple fields then your error messages should let the user know which field they need to fix.