ArtiGrid
Conditional logic
The Field Conditions logic in ArtiGrid allows dynamic rules to be applied to form fields directly from PHP without writing JavaScript manually.
Multiple conditions can be chained on the same grid. In this example, three independent rules are defined: $grid->fieldCondition('email', 'officeCode', '==', 4, 'hide') hides the email field when Chicago is selected, $grid->fieldCondition('extension', 'officeCode', '==', 1, 'hide') hides the extension field when New York is selected, and $grid->fieldCondition('jobTitle', 'officeCode', '==', 2, 'hide') hides the jobTitle field when San Francisco is selected.
Each condition specifies a target field, a dependent field (officeCode), an operator, a comparison value, and an action (hide or show). When the form is rendered, all rules are serialized to JSON and injected into the frontend through the data-field-conditions attribute.
In the browser, JavaScript reads every condition, binds a change listener to the dependent field (officeCode), and evaluates each rule in real time whenever the user selects a different office.
For example, selecting Chicago (value 4) hides the email field, selecting New York (value 1) hides the extension field, and selecting San Francisco (value 2) hides the jobTitle field. Switching to any other option restores all fields automatically.
The evaluation also runs once when the form first loads, so fields are already in the correct state before the user interacts with anything.
In edit mode, the existing record data is used for the initial evaluation. If a condition is already satisfied based on the saved values, the corresponding field is hidden from the moment the form opens, with no user interaction required.
This allows the creation of fully dynamic forms that adapt in both insert and edit mode without page reloads and without any additional backend or frontend logic.
<?php
$grid = new ArtiGrid();
$grid->table('employees')
->template('bootstrap5')
->fieldCondition('email', 'officeCode', '==', 4, 'hide')
->fieldCondition('extension', 'officeCode', '==', 1, 'hide')
->fieldCondition('jobTitle', 'officeCode', '==', 2, 'hide')
->combobox("officeCode", [
"1" => "New York",
"2" => "San Francisco",
"3" => "Boston",
"4" => "Chicago"
]);
echo $grid->render();
?>