Skip to content

Custom conditions missing prefix on linked fields #8439

Description

@ilicmarko

Bug description

Note: This is probably more a feature request but also it makes custom conditions unusable for linked fields.

If you have a fieldset with a custom condition and you link that fieldset with a prefix there isn't a way for you to know dynamically what the prefix is.

Here is the following diagram to better explain it:

image

Inside the blueprint, two validations will run and the values parameter will return an object with first_my_field and second_my_field. The problem is you are not aware of what field the validation is running, you don't know the prefix, therefore, you can't write a rule that is only scoped to my_fieldset.
In the ideal scenario, you would have a prefix and you could use it to craft a dynamic condition. For example:

Statamic.$conditions.add("shouldShow", ({ values }) => {
  const fieldPrefix = values.prefix
  
  return values[`${fieldPrefix}my_field`] === "something";
});

The function above would only validate in the context of that exact linked field.

The use case for this is: imagine that you have multiple fields inside my_fieldset and you need only fields inside that fieldset to affect the choice. This is the same behavior that the UI filters use, they only apply scoped to the linked field.

image

How to reproduce

  1. Create a new fieldset my_fieldset
  2. Inside the fieldset create a new field (type not relevant), my_field
  3. Set a custom method passes condition on my_field, let the value by shouldShow
  4. Create a new collection/blueprint pages
  5. Add two linked fieldsets fields my_fieldset and set a different prefix for both
  6. Now when you open to create a new pages collection you will get two validations with the same values

Logs

No response

Environment

Environment
Application Name: Statamic
Laravel Version: 10.14.1
PHP Version: 8.2.3
Composer Version: 2.5.8
Environment: local
Debug Mode: ENABLED
URL: dummy-statamic.ddev.site
Maintenance Mode: OFF

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

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 0
Antlers: runtime
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.9.2 Solo

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

regex (default)

Additional details

No response

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