Skip to content

Field conditions don't work with Entry fields #11915

Closed

Description

@adampatterson

Bug description

If I want to show or hide another field based on the value of an Entry Field the comparisons don't seem to work.

My hunch is that the comparison is between an integer and an integer as a string.

This could also be a result of using the Eloquent Driver, but I'm not sure.

These tests will fail.

expect(showFieldIf({ string_age: "== 13" })).toBe(true);  // Fail

expect(showFieldIf({ age: "=== 13" })).toBe(false); // Pass
expect(showFieldIf({ string_age: "=== 13" })).toBe(true); //  Pass ?
expect(showFieldIf({ string_age: "=== 13" })).toBe(true); //  Fail

expect(showFieldIf({ string_age: "equals 13" })).toBe(true); // Fail
expect(showFieldIf({ string_age: "equals 13" })).toBe(false); // Pass

What I'm not clear on though is how the right hand value is interpreted.

I feel like the condition is maybe more like 123 === "123"

For example, if I console log from within the Validator.js in prepareCondition I can see that the left hand value is an integer, and the right hand value is a string.

lhs:  [0: 25863]
rhs: "25863"

If I console log from passesConditions then I get something like:

field: "archive"
operator: "==="
value: "25863"

I've tried using in, contains, equal, and ===

I ended up writing my own condition and then using ParsIn on the Values coming in, I also added it to the Params but they were already ints.

The Left hand values also seem to be an array of IDs even when choosing a max of 1. So the comparisons could be completely off then.

How to reproduce

Using the Eloquent Driver might make a difference.

Create a fieldset using an Entry Field Max items 1
Create any other Field and then set the condition to show if the Entry Field is equal to t he ID from the entry.

In the editor, select the entry that matches the ID chosen in the condition.

The Field won't show.

Logs

Environment

Environment
Application Name: Tourism Calgary
Laravel Version: 11.45.1
PHP Version: 8.4.7
Composer Version: 2.8.9
Environment: adam
Debug Mode: ENABLED
URL: tourism-calgary.adam.paperleaf.dev
Maintenance Mode: OFF
Timezone: America/Edmonton
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: reverb
Cache: redis
Database: mysql
Logs: stack / single
Mail: log
Queue: database
Scout: algolia
Session: redis

Sentry
Enabled: YES
Environment: adam
Laravel SDK Version: 4.15.0
PHP SDK Version: 4.14.0
Release: NOT SET
Sample Rate Errors: 100%
Sample Rate Performance Monitoring: NOT SET
Sample Rate Profiling: NOT SET
Send Default PII: DISABLED

Statamic
Addons: 6
Sites: 1
Stache Watcher: Disabled (auto)
Static Caching: Disabled
Version: 5.57.0 PRO

Statamic Addons
aryehraber/statamic-captcha: 1.14.0
eminos/statamic-tabs: 1.2.0
statamic/collaboration: 1.0.1
statamic/eloquent-driver: 4.25.2
statamic/seo-pro: 6.7.0
thoughtco/statamic-restrict-fields: 1.1.0

Statamic Eloquent Driver
Asset Containers: eloquent
Assets: eloquent
Blueprints: eloquent
Collection Trees: eloquent
Collections: eloquent
Entries: eloquent
Forms: eloquent
Global Sets: eloquent
Global Variables: eloquent
Navigation Trees: eloquent
Navigations: eloquent
Revisions: eloquent
Sites: eloquent
Taxonomies: eloquent
Terms: eloquent
Tokens: eloquent

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

Activity

  1. adampatterson commented on Jul 1, 2025

    @adampatterson
    ContributorAuthor

    This indicates that you're only casting to a number when the operator is one of the following:

    [">", ">=", "<", "<="]

  2. duncanmcclean commented on Jul 2, 2025

    @duncanmcclean
    Member

    Are you able to provide a real example of a field condition which isn't working?

  3. adampatterson commented on Jul 2, 2025

    @adampatterson
    ContributorAuthor

    @duncanmcclean the YML?

    I can export it if you like.

    This indicates that you're only casting to a number when the operator is one of the following:

    [">", ">=", "<", "<="]

    Further exploration inside prepareLhs() when I log typeof on lhs is an object which returns it back as an object.

    So I think the comparison being done would be object == string

    If I change the return statement to use the first array key then the condition evaluates to true.

  4. duncanmcclean commented on Jul 2, 2025

    @duncanmcclean
    Member

    the YML?

    I can export it if you like.

    Yes, please.

  5. adampatterson commented on Jul 4, 2025

    @adampatterson
    ContributorAuthor

    @duncanmcclean here's my YAML, I added some extras to see if they worked, but they didn't.

    I've also included the custom validation that I ended up using.

    Statamic.$conditions.add("isArchive", ({ target, params, values, root }) => {
        // Parse params as an array of numbers
        const paramNumbers = Array.isArray(params)
            ? params.map((p) => parseInt(p, 10)).filter((n) => !isNaN(n))
            : [];
    
        // Parse archive value as a number
        const archiveValue =
            values && values.archive && values.archive.length > 0
                ? parseInt(values.archive[0], 10)
                : null;
    
        // Return true if archiveValue matches any param
        return (
            typeof archiveValue === "number" && paramNumbers.includes(archiveValue)
        );
    });
    title: 'Block - Prefiltered Archive Links'
    fields:
      - field:
          type: text
          display: Title
        handle: title
      - field:
          type: tab
          display: Content
          sortable: false
        handle: tab_content
      - field:
          type: bard
          buttons:
            - bold
            - italic
            - unorderedlist
            - orderedlist
            - removeformat
            - anchor
          display: Content
          save_html: true
          remove_empty_nodes: false
        handle: content
      - field:
          type: assets
          display: Image
          container: media
          max_files: 1
        handle: image
      - field:
          sets:
            new_set_group:
              sets:
                link:
                  fields:
                    - field:
                        type: text
                        display: Title
                        instructions: 'If left empty, the archive title will be used.'
                      handle: title
                    - field:
                        mode: select
                        type: entries
                        display: 'Choose Target Archive'
                        max_items: 1
                        collections:
                          - archives
                      handle: archive
                    - field:
                        if:
                          archive: 'equals 25863'
                        type: terms
                        create: false
                        display: 'TEST Equal'
                        max_items: 1
                        taxonomies:
                          - event_type
                        always_save: true
                      handle: test_equal
                    - field:
                        if:
                          archive: '=== 25863'
                        type: terms
                        create: false
                        display: 'TEST Strict equality'
                        max_items: 1
                        taxonomies:
                          - event_type
                        always_save: true
                      handle: test_strict_equality
                    - field:
                        if:
                          archive: 'contains 25863'
                        type: terms
                        create: false
                        display: 'TEST Contains'
                        max_items: 1
                        taxonomies:
                          - event_type
                        always_save: true
                      handle: test_contains
                    - field:
                        if:
                          archive: 'contains_any 25863'
                        type: terms
                        create: false
                        display: 'TEST Contains Any'
                        max_items: 1
                        taxonomies:
                          - event_type
                        always_save: true
                      handle: test_contains_any
                    - field:
                        if:
                          archive: 'custom isArchive:25863'
                        type: terms
                        create: false
                        display: 'TEST Custom'
                        max_items: 1
                        taxonomies:
                          - event_type
                        always_save: true
                      handle: test_custom
                    - field:
                        if: 'isArchive:13055,13056,25861'
                        type: entries
                        create: false
                        display: Structure
                        max_items: 1
                        always_save: true
                        collections:
                          - structure
                      handle: structure
                    - field:
                        if: 'isArchive:25863'
                        type: terms
                        create: false
                        display: 'Event Type'
                        max_items: 1
                        taxonomies:
                          - event_type
                        always_save: true
                      handle: event_type
                    - field:
                        if: 'isArchive:25862'
                        type: terms
                        create: false
                        display: Categories
                        max_items: 1
                        taxonomies:
                          - categories
                        always_save: true
                      handle: categories
                    - field:
                        if: 'isArchive:25862'
                        type: terms
                        create: false
                        display: Region
                        max_items: 1
                        taxonomies:
                          - areas
                        always_save: true
                      handle: region
                    - field:
                        if: 'isArchive:25862'
                        type: terms
                        create: false
                        display: 'Scenario Tags'
                        max_items: 1
                        taxonomies:
                          - scenario_tags
                        always_save: true
                      handle: scenario_tags
                  display: Link
              display: 'New Set Group'
          type: replicator
          display: 'Prefiltered Links'
          button_label: 'Add Link'
        handle: prefiltered_links
      - import: toggle_beads
      - field:
          type: tab
          display: Options
          sortable: false
        handle: tab_options
      - import: block_options
    
  6. duncanmcclean commented on Nov 16, 2026

    @duncanmcclean
    Member

    Thanks for the blueprint, that helped track it down!

    Even with max_items: 1, an Entries field's value is always an array of IDs on the publish form, which is where field conditions are evaluated. So equals and === won't ever match, since they compare the whole array against your value. For array values, you'll want to use contains instead:

    if:
      archive: 'contains 25863'

    However, as you noticed, contains wasn't working either. The Eloquent driver gives entry IDs as numbers ([25863]), but the condition's value is always a string ("25863"), so it never matched. #15936 fixes that, along with contains_any / includes_any, e.g. archive: 'contains_any 13055, 13056, 25861'.

    Once that's released, you should be able to replace your custom isArchive condition with contains / contains_any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions