Tutorial: hide, grey out or lock with conditions
This tutorial shows the three ways pgkiln makes a button or field depend on the data:
| You want | Use | Where it is decided |
|---|---|---|
| The button is not there at all | a server-side condition | on the server, for every page view, and again when it is pressed |
| The button is there but grey until something is filled in | an enable dynamic action with a client-side condition | in the browser, while the user types |
| A field is shown but can't be changed | a read-only condition | on the server, for every page view and submit |
It builds on the Tasks app of the Tasks tutorial: page 1 is the My tasks report, page 2 the modal form with the items P2_ID, P2_TITLE, P2_PRIORITY and P2_DUE_DATE, and the buttons CREATE, SAVE, DELETE and COMPLETE (Mark done). Run examples/tasks-app.sql if you don't have it yet.
Step 1: a button that only exists for an existing record
The form wizard already did this for you: Create is only there for a new task, Apply Changes (SAVE) and Delete only for an existing one. Open page 2 in the Page Designer, select the DELETE button and look at its Server-side condition group: the expression is the SQL :P2_ID is not null. The Advisor suggests saying the same with a type, which needs no query:
| Property | Value |
|---|---|
| Condition type | item_not_null |
| Expression / item | P2_ID |
The tree shows the condition in words on the button's badge. For a new task P2_ID is empty, so the button is not rendered.
This is more than cosmetics. pgkiln checks the condition again when the button is pressed: a forged request with DELETE for a new task is refused (403) and logged. That is why a condition is the right tool whenever pressing the button in the wrong state would be wrong.
Step 2: a condition on the data
Mark done should only be there while the task is open. That needs the database, so it's a sql condition (step 5 of the Tasks tutorial typed it as a plain condition, which means the same):
| Property | Value |
|---|---|
| Condition type | sql |
| Expression / item | :P2_ID is not null and not exists (select 1 from tasks.task where id = :P2_ID::int and done) |
or, with the type not_exists and only the query:
| Property | Value |
|---|---|
| Condition type | not_exists |
| Expression / item | select 1 from tasks.task where id = :P2_ID::int and (done or :P2_ID is null) |
Open a done task: Mark done is gone. The query runs as the application's database role, so row level security applies: a condition never sees rows the user can't see.
A few other types you'll use often (the full list is in chapter 6):
| Condition type | Example | Holds when |
|---|---|---|
item_equals | P2_PRIORITY / HIGH | the item has that value |
item_in_list | P2_PRIORITY / NORMAL:HIGH | the item has one of the values (colon separated) |
request_in | SAVE,CREATE | one of these buttons was pressed (for validations and processes) |
page_is_dialog | the page is open as a modal dialog | |
user_signed_in | the user is signed in (not the public user) |
The same condition group exists on regions, items, validations, processes, computations, branches and dynamic actions. A region whose condition fails is not rendered, together with its items and buttons.
Step 3: a button that is grey until the form is filled in
Sometimes the button should stay visible, so the user knows it is there, but only work once the form is complete. That is a dynamic action. On page 2 add one:
| Property | Value |
|---|---|
| Name | Create once there is a title |
| Event | change |
| Item(s) | P2_TITLE |
| Client-side condition | is_not_null |
| Action | enable |
| Affected items | CREATE,SAVE |
enable with a condition does the opposite when the condition is false: while the title is empty, Create and Save are greyed out (disabled); as soon as the user types a title, they light up. The dynamic action also runs when the page loads, so a new task opens with Create already grey.
Several fields? Name them all as trigger items and choose the condition all_not_null: the button is enabled only when every one of them has a value.
| Property | Value |
|---|---|
| Item(s) | P2_TITLE,P2_DUE_DATE |
| Client-side condition | all_not_null |
Link buttons (action redirect or document) can be affected items too: a greyed-out link gets aria-disabled and can't be clicked. The HR example greys Print on page 3 while the employee's name is empty (examples/hr/hr_125_link_button_until_filled.sql) and Request leave on page 37 until both days are filled in (examples/hr/hr_124_button_until_filled.sql).
Grey is a convenience, not a rule. A dynamic action runs in the browser, and anyone can enable a button with the browser's developer tools. Keep the real rule on the server: the column is
not null, and a validation or the button's server-side condition refuses what must not happen.
Step 4: a field that can't be changed any more
A done task should still show its due date, but nobody should change it. Select P2_DUE_DATE and set Read-only condition:
exists (select 1 from tasks.task where id = :P2_ID::int and done)The item is now shown as text for done tasks. A read-only item is never taken from the submitted form, so even a forged request can't change it. If the condition raises an error, the item is treated as read-only (fail closed).
To make a whole form read-only, give the region a Read only condition instead (type exists, the query select 1 from tasks.task where id = :P2_ID::int and done): while it holds, every item of the region and its sub regions is read-only (chapter 4). Hide Apply Changes with the opposite condition, so a done task has nothing to save.
Step 5: show or hide while the user types
The last tool is show / hide: like enable, but the field or region disappears. Add a dynamic action that shows the due date only for high-priority tasks:
| Property | Value |
|---|---|
| Event / Item(s) | change / P2_PRIORITY |
| Client-side condition | equals HIGH |
| Action | show |
| Affected items | P2_DUE_DATE |
show and hide are also applied on the server when the page is drawn, so there is no flicker. Like greying out, they are for convenience; a hidden item is still submitted.
Which one to choose
- The user may not do it in this state: a server-side condition on the button (and, for data, a check in the database).
- The user can't do it yet because something is missing: grey it with an
enabledynamic action, and keep a validation on the server. - The value may not change any more: a read-only condition.
- The field is irrelevant for this choice: show/hide with a dynamic action.
Who the user is (a manager, an administrator) is not a condition but an authorization scheme: see the next tutorial, buttons per role.