Page Rules
Page rules allow you to create automatic actions that trigger when specific conditions are met as soon as a user opens a page. They're powerful tools for creating dynamic, responsive pages that adapt to user roles and data conditions.
What You'll Learn
This article walks you through the concept of page rules, showing how they can automatically hide elements, display messages, or redirect users based on conditions you define. You'll discover how to set up conditions using data fields, configure different action types, and manage multiple rules effectively.
What are page rules?
Page rules are actions that can be automatically triggered based on conditions you define as soon as a user opens a page.
For example, consider a scenario where users can register for an event with limited availability. In such cases, you can employ a page rule to automatically hide the registration form when the availability reaches zero.
Here are a few additional instances that demonstrate how you can make use of page rules:
- Hide a form to edit a record if the user role of the logged-in user is not an "Editor"
- Hide a form to register for a class if the class attendance has exceeded a specific size
- Redirect to a registration form if the logged-in user hasn't registered
- Warn the user if they need to update their data or complete some information
Note: Page rules can be applied to child pages or start pages with user logins enabled (protected pages). However, it is important to note that page rules cannot be triggered based on values in multi-record elements such as grids, lists, maps, calendars, and so on.
Managing Page Rules
Adding Page Rules
You can access page rules in the Builder by selecting the "Rules" tab on the top menu. When you select this option, the page rule settings will be displayed in the toolbox on the left side of the screen.
Note: If you do not see the "Rules" tab at the top menu, that is because the page you are currently previewing does not require users to login to access the page.
From there, you can add a new rule by selecting "Add Rule".
INSERT IMAGE PLACEHOLDER
Editing a Page Rule
Existing page rules can be accessed and edited at any time from the same down arrow menu option next to the page's name or by selecting "Rules" on the top menu. To modify a page rule, you have the flexibility to add or remove any criteria using the "+" or "-" icons.
INSERT IMAGE PLACEHOLDER
You can drag/drop each individual rule using the left anchor icon to keep your rules organized. You must press the "Save Changes" button in order for your changes to take effect.
INSERT IMAGE PLACEHOLDER
Deleting Page Rules
To get rid of a page rule entirely, use the trashcan delete icon to the right of the rule panel. You must press the "Save Changes" button in order to save your changes permanently.
INSERT IMAGE PLACEHOLDER
Building Page Rules
Defining the Conditions
Page rules are triggered when certain conditions that you specify are met during the loading of a page. These conditions are added in the section titled "When the following criteria is true".
To define your conditions, you select a specific field and then specify the criteria for that field. For example, if you want to display a message when a status field is set to overdue, the criteria would need to check if the status field is indeed set to overdue in order for the rule to be triggered.
The fields available to use will depend on the page:
- Child pages will let you choose fields from the table that page is based on, or a connected table's fields
- If the page is protected by a login, you will also be able to use fields from the account table and other user roles associated with that page's login
In other words, fields from the source table of the page as well as fields from the user roles that have access to the page will be available to use in your page rules. If no fields are available, the "Rules" link will not be available.
You will need to check for at least one condition, but you can add as many as you need in a single rule. If you have multiple fields selected, they must all be true in order for the rule to trigger.
INSERT IMAGE PLACEHOLDER
Defining the Action
Each rule can perform only one action. To add an action, go to the section titled "Run the following action" and select the action that you want your page rule to perform.
There are four main actions a page rule can perform:
- Hide elements
- Show a message
- Redirect to an existing page in your app
- Redirect to another website URL
INSERT IMAGE PLACEHOLDER
Hiding Elements
This action type will hide any selected elements when your criteria match.
INSERT IMAGE PLACEHOLDER
Show a Message
The "Show a message option" will display a message of your choice. The message will be displayed at the top of the page, above any elements.
INSERT IMAGE PLACEHOLDER
There are four different messaging styles available; they each have a unique background color for different effects:
- Neutral
- Confirmation
- Warning
- Red Alert
INSERT IMAGE PLACEHOLDER
You can choose whether the message can be dismissed by the user or not. If toggled, an "X" icon on the top-right corner of the message will be displayed.
Redirect to the Parent Page
Once this action is set, the user will be redirected back to the page that the child page originates from (otherwise referred to as the parent page).
INSERT IMAGE PLACEHOLDER
Note: This action is only available for child pages. To learn more about page types, see our article here.
Redirect to an Existing Page
This action will redirect the user to a specific page within the app of your choosing. All starting pages are available as well as any other child page that has the same table as a source. If the child page is meant to display a specific record, the same record as the current page will be used.
INSERT IMAGE PLACEHOLDER
Redirect to Another Website URL
With this action, you can add an external website URL to redirect your users to.
INSERT IMAGE PLACEHOLDER
Note: There are no "back" crumbtrail links generated when this option is selected, so if you're linking to an external website, the only way back to your Knack page is by using the browser's back and forward arrow buttons.
How Multiple Rules Work
It is important to understand that when you have multiple rules, all matching rules will be triggered simultaneously. This applies even to rules that have the same criteria.
Knack does not stop triggering rules after the first match.
Take the following scenario for example:
INSERT IMAGE PLACEHOLDER
In the above image, there are two page rules that will trigger when the Manager's user status is "active". The first rule will hide an element and the second rule will display a message.
In this scenario, both rules will trigger, so the user will see a message and the element will be hidden as well. If we were to tweak the above scenario, the second rule will now redirect the user instead:
INSERT IMAGE PLACEHOLDER
Technically, both rules are still going to trigger, but since the user is being redirected out of the page, they are never going to see the elements on this page anyway.
Tip: In this knowledge article, you will find a detailed step-by-step guide on how to create a registration form with limited availability. Additionally, it will explain how to utilize page rules to automatically hide the form and display a message when the availability reaches zero.
Notes & Troubleshooting
- If you have multiple fields selected in your page rules, the conditions must all be true in order for the rule to trigger.
- Page rules run before elements on the page are rendered in the Live App. This allows for re-directs to be put in motion or only "show" elements to be displayed.
Updated 9 days ago