Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
12bcb0d
feat: implement conditional logic for submission form fields (initial…
iamibrahimriaz Dec 3, 2025
08bc39c
conditional logic builder style - updated
iamibrahimriaz Dec 4, 2025
ae44906
Merge branch 'development' of https://github.com/sovware/directorist …
iamibrahimriaz Dec 8, 2025
78721d7
Conditional Logic on frontend - updated
iamibrahimriaz Dec 8, 2025
b7854b6
condition on wp_editor - added
iamibrahimriaz Dec 8, 2025
b63915c
Developer docs - updated
iamibrahimriaz Dec 8, 2025
cc5e4d2
conditional_logic helper - updated
iamibrahimriaz Dec 9, 2025
8dafc69
assign_to category - removed
iamibrahimriaz Dec 9, 2025
3e07566
fix: conditional logic for category field selection
iamibrahimriaz Dec 10, 2025
b772e10
fix: conditional logic support for tag and location fields
iamibrahimriaz Dec 10, 2025
bbb67cf
decodeHtmlEntities - added on builder select field
iamibrahimriaz Dec 10, 2025
a048a15
fix: conditional logic operator (is) behavior
iamibrahimriaz Dec 10, 2025
bb4a158
condition select field style - updated
iamibrahimriaz Dec 10, 2025
8f23fc5
fix: conditional logic for custom select fields
iamibrahimriaz Dec 10, 2025
2adb9d1
fix: conditional logic for custom checkbox fields
iamibrahimriaz Dec 11, 2025
9393097
fix: conditional logic for custom radio fields
iamibrahimriaz Dec 11, 2025
0e65e65
fix: conditional logic for wp-color-picker field
iamibrahimriaz Dec 11, 2025
04505dd
Filter out date & time type field from select-field lit
iamibrahimriaz Dec 11, 2025
699802a
exclude fields - updated
iamibrahimriaz Dec 11, 2025
734aec5
fix: date, time, file type issue - fixed
iamibrahimriaz Dec 11, 2025
052aa86
Merge branch 'development' of https://github.com/sovware/directorist …
iamibrahimriaz Dec 15, 2025
07b65a0
updated: operator based on fieldType
iamibrahimriaz Dec 15, 2025
fd19ba4
feat: listing_img field condition added
iamibrahimriaz Dec 15, 2025
9cbdadf
feat: Dynamic operator filtering by field type
iamibrahimriaz Dec 15, 2025
62bf6df
feat: conditional logic updated for select, checkbox & radio field
iamibrahimriaz Dec 15, 2025
d611e8e
feat: privacy_policy filed condition - updated
iamibrahimriaz Dec 15, 2025
293a8c3
feat: color field conditional logic updated
iamibrahimriaz Dec 15, 2025
a3dcff2
Merge branch 'development' of https://github.com/sovware/directorist …
iamibrahimriaz Dec 15, 2025
c3af51a
Merge branch 'development' of https://github.com/sovware/directorist …
iamibrahimriaz Dec 18, 2025
5830854
feedback - improved
iamibrahimriaz Dec 18, 2025
c6e79f6
Merge branch 'development' of https://github.com/sovware/directorist …
iamibrahimriaz Jan 4, 2026
47cda2f
Category, Tag, Location field updated for "directorist-conditional-lo…
iamibrahimriaz Jan 4, 2026
2c8e4ad
Filtering current field from add rule
iamibrahimriaz Jan 4, 2026
b66a2b4
Added clear button on select type conditional-logic-value
iamibrahimriaz Jan 5, 2026
6d641a2
Link with WhatsApp moved top of Conditional Logic
iamibrahimriaz Jan 5, 2026
7b1080d
Text (Button, Options) - updated
iamibrahimriaz Jan 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
279 changes: 279 additions & 0 deletions CONDITIONAL_LOGIC_QUICK_REFERENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
# Conditional Logic - Quick Reference Guide

## 🎯 What It Does

Conditional Logic allows form fields to **show or hide automatically** based on other field values.

**Example:** Show "Menu" field only when "Category" is "Restaurant"

---

## 🚀 Quick Start

### For Backend Developers

#### Add to Field Template (2 steps)

**Step 1:** Add at top of template file:

```php
<?php
$listing_form = directorist()->listing_form;
$conditional_logic_attr = $listing_form->get_conditional_logic_attributes($data);
?>
```

**Step 2:** Add to wrapper div:

```php
<div class="directorist-form-group" <?php echo $conditional_logic_attr; ?>>
<!-- Your field content -->
</div>
```

**That's it!** ✨

---

## 📋 Conditional Logic Structure

```php
$conditional_logic = [
'enabled' => true, // Must be true
'action' => 'show', // 'show' or 'hide'
'globalOperator' => 'OR', // 'AND' or 'OR' - how to combine groups (defaults to OR)
'groups' => [
[
'operator' => 'AND', // 'AND' or 'OR' - how to combine conditions within group
'conditions' => [
[
'field' => 'category', // Field key (widget_key like 'title' auto-maps to 'listing_title')
'operator' => 'is', // See operators below
'value' => 'Restaurant' // Value to compare
]
]
]
]
];
```

---

## 🔧 Supported Operators

| Operator | Usage | Example |
| ----------------------- | --------------- | -------------------------------- |
| `is` | Exact match | Category is "Restaurant" |
| `is not` | Not equal | Category is not "Hotel" |
| `contains` | Contains text | Description contains "delicious" |
| `does not contain` | Doesn't contain | Title doesn't contain "test" |
| `empty` | Field is empty | Phone is empty |
| `not empty` | Field has value | Email is not empty |
| `greater than` | Number > | Price > 100 |
| `less than` | Number < | Price < 50 |
| `greater than or equal` | Number >= | Price >= 100 |
| `less than or equal` | Number <= | Price <= 50 |
| `starts with` | Text starts | Title starts with "Best" |
| `ends with` | Text ends | Title ends with "2024" |

---

## 🎨 Logic Rules

### Groups = globalOperator Logic

- **OR** (default): If **ANY** group matches → field shows/hides
- **AND**: If **ALL** groups match → field shows/hides

### Conditions in Group

- **AND** = ALL conditions must match
- **OR** = ANY condition must match

### Example (OR between groups):

```
Group 1: (Category = Restaurant AND Type = Fine Dining)
Group 2: (Category = Cafe)
globalOperator: OR

Result: Show field if Group 1 OR Group 2 matches
= (Restaurant AND Fine Dining) OR (Cafe)
```

### Example (AND between groups):

```
Group 1: (Category = Restaurant)
Group 2: (Price > 100 OR Rating > 4)
globalOperator: AND

Result: Show field if Group 1 AND Group 2 match
= (Restaurant) AND (Price > 100 OR Rating > 4)
```

---

## 📁 File Locations

| What | Where |
| --------------- | ----------------------------------------------------------------------- |
| Helper Function | `includes/model/ListingForm.php` → `get_conditional_logic_attributes()` |
| Frontend Module | `assets/src/js/global/components/conditional-logic.js` |
| Main Form JS | `assets/src/js/global/add-listing.js` |
| Field Templates | `templates/listing-form/fields/` or `custom-fields/` |

---

## 🔍 Common Examples

### Show field when category is Restaurant

```php
'groups' => [
[
'operator' => 'AND',
'conditions' => [
[
'field' => 'category',
'operator' => 'is',
'value' => 'Restaurant'
]
]
]
]
```

### Show field when price > 100

```php
'groups' => [
[
'operator' => 'AND',
'conditions' => [
[
'field' => 'price',
'operator' => 'greater than',
'value' => '100'
]
]
]
]
```

### Hide field when category is empty

```php
'action' => 'hide', // Hide instead of show
'groups' => [
[
'operator' => 'AND',
'conditions' => [
[
'field' => 'category',
'operator' => 'empty',
'value' => ''
]
]
]
]
```

### Multiple conditions (ALL must match)

```php
'groups' => [
[
'operator' => 'AND',
'conditions' => [
['field' => 'category', 'operator' => 'is', 'value' => 'Restaurant'],
['field' => 'type', 'operator' => 'is', 'value' => 'Fine Dining']
]
]
]
```

### Multiple conditions (ANY can match)

```php
'groups' => [
[
'operator' => 'OR',
'conditions' => [
['field' => 'category', 'operator' => 'is', 'value' => 'Restaurant'],
['field' => 'category', 'operator' => 'is', 'value' => 'Cafe']
]
]
]
```

### Multiple groups (OR logic)

```php
'groups' => [
// Group 1
[
'operator' => 'AND',
'conditions' => [
['field' => 'category', 'operator' => 'is', 'value' => 'Restaurant']
]
],
// Group 2 (if Group 1 doesn't match, try this)
[
'operator' => 'AND',
'conditions' => [
['field' => 'category', 'operator' => 'is', 'value' => 'Cafe']
]
]
]
// Result: Show if Restaurant OR Cafe
```

---

## ✅ Checklist

### Backend

- [ ] Added `get_conditional_logic_attributes($data)` call
- [ ] Added `<?php echo $conditional_logic_attr; ?>` to wrapper div
- [ ] Conditional logic is in `$data['options']['conditional_logic']`
- [ ] `enabled` is `true`
- [ ] Field key matches in conditions

### Frontend (if adding new field type)

- [ ] Field selector added to `mapFieldKeyToSelector()`
- [ ] Field value can be retrieved correctly

---

## 🐛 Troubleshooting

| Problem | Solution |
| ------------------------ | ---------------------------------------------------------- |
| Field not showing/hiding | Check `enabled: true` |
| Field always hidden | Check condition field key matches |
| Condition not working | Verify field value format matches |
| Invalid JSON error | Check JSON structure is valid (HTML entities auto-decoded) |
| Field not detected | Add to `mapFieldKeyToSelector()` |
| TinyMCE not triggering | Ensure editor is within `.directorist-form-group` |
| Operator not working | Operators are case-insensitive, check normalization |
| Widget key mismatch | Use widget_key (e.g., `title`) - auto-maps to field_key |

---

## 💡 Pro Tips

1. **Test in browser console:** Inspect `data-conditional-logic` attribute
2. **Use AND for strict rules:** All conditions must match
3. **Use OR groups for flexibility:** Multiple ways to show field
4. **Field keys are case-sensitive:** "Category" ≠ "category"
5. **Empty values:** Use `empty` operator, not `is` with empty string
6. **Widget keys auto-map:** `title` → `listing_title`, `description` → `listing_content`
7. **Operators are case-insensitive:** "AND" = "and" = "And"
8. **TinyMCE fields work:** Both textarea and wp_editor fields are supported

---

**Need more details?** See `CONDITIONAL_LOGIC_REFACTORING.md`
Loading
Loading