8000 Fieldsets cannot validate against a nested fieldset's value. · Issue #11721 · statamic/cms · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fieldsets cannot validate against a nested fieldset's value. #11721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
finnjsmith opened this issue Apr 22, 2025 · 4 comments
Open

Fieldsets cannot validate against a nested fieldset's value. #11721

finnjsmith opened this issue Apr 22, 2025 · 4 comments

Comments

@finnjsmith
Copy link
finnjsmith commented Apr 22, 2025

Bug description

I attempted to include some validation where I check that an imported fieldset (link_type) within a fieldset (card, used as a set in a bard field in this instance) has a field containing certain value. This is so that some fields are only visible / validated when specific data in the imported fieldset is present. The issue is, the validation logic completely fails here, and didn't seem to be applying the correct checks.

Some context for the screenshot: "Link Type" (handle link_type) is one of a number of fields imported as a fieldset into the card fieldset. The screenshot shows the card fieldset being used as a set within a bard field.
We can see below that the image field is failing validation when the "Link Type" is equal to "Page / Story" (label for collection), despite the image's validation looking like this:

validate:
  - sometimes
  - 'required_unless:link_type,collection'

Image

See my initial message on the Statamic discord here.

How to reproduce

In my specific setup, I have an assets field called image inside of the card fieldset which I am using as a set inside a bard field. image should only be required if the link_type field is not equal to "collection". link type is contained within another fieldset that's imported into card, called optional_type_link, so we only need to focus on these two fields (I'll include the entire fieldset for context though).

So in order to reproduce, we need to import a second fieldset (optional_type_link) into the original fieldset card, and then use the first fieldset within a bard field. The validation of the nested fields will fail. I know that I have the option to just add the fields directly to card, but I want to make my project dynamic, and there are multiple places that require these fields.

resources/fieldsets/card.yaml:

title: Card
fields:
  -
    import: set_type_selector
  -
    handle: image
    field:
      max_files: 1
      container: assets
      type: assets
      display: Image
      localizable: false
      validate:
        - sometimes
        - 'required_unless:link_type,collection'
        - nullable
      listable: false
      sortable: false
      if:
        set_type: 'equals image'
  -
    import: icon_selector
  -
    handle: heading
    field:
      type: text
      display: Heading
      localizable: false
      validate:
        - required
        - sometimes
      if:
        set_type: 'not empty'
      listable: false
      sortable: false
  -
    handle: subheading
    field:
      type: textarea
      display: Subheading
      localizable: false
      listable: false
      sortable: false
      if:
        set_type: 'not empty'
  -
    import: optional_type_link
  -
    handle: call_to_action
    field:
      type: toggle
      display: 'Call to action'
      localizable: false
      validate:
        - sometimes
      if:
        set_type: 'not empty'
      listable: false
      sortable: false
  -
    handle: call_to_action_text
    field:
      type: text
      display: 'Call to action text'
      localizable: false
      validate:
        - required
        - sometimes
      if:
        set_type: 'not empty'
        call_to_action: 'equals true'
      listable: false
      sortable: false

optional_type_link:

fields:
  -
    handle: link_type
    field:
      options:
        -
          key: url
          value: URL
        -
          key: collection
          value: Page/Story
        -
          key: download
          value: Download
      placeholder: 'Please select'
      type: select
      display: 'Link type'
      localizable: false
      validate:
        - required
        - sometimes
      listable: false
      sortable: false
      if:
        set_type: 'not empty'
      width: 50
  -
    handle: target
    field:
      options:
        -
          key: _self
          value: 'Current tab / window'
        -
          key: _blank
          value: 'New tab / window'
      searchable: false
      default: _self
      type: select
      display: 'Link target'
      localizable: false
      validate:
        - required
      width: 50
  -
    handle: link
    field:
      type: text
      display: Link
      localizable: false
      validate:
        - sometimes
        - required
      listable: false
      sortable: false
      if:
        link_type: 'equals url'
  -
    handle: download
    field:
      max_files: 1
      container: assets
      type: assets
      display: Download
      localizable: false
      validate:
        - sometimes
        - required
      listable: false
      sortable: false
      if:
        link_type: 'equals download'
  -
    handle: page_story
    field:
      max_items: 1
      type: entries
      display: Page/Story
      localizable: false
      validate:
        - sometimes
        - required
      listable: false
      sortable: false
      collections:
        - pages
        - stories
      if:
        link_type: 'equals collection'
title: 'Optional type link'

We can see below that the image field is failing validation when the "Link Type" is equal to "Page / Story" (label for the collection handle).
Image

Logs

Environment

Environment
Application Name: REDACTED
Laravel Version: 12.8.1
PHP Version: 8.3.19
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: REDACTED.test
Maintenance Mode: OFF
Timezone: UTC
Locale: en

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

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

Storage
public/storage: LINKED

Statamic
Addons: 6
Sites: 13 (International, Australia, Belgium, and 10 more)
Stache Watcher: Enabled
Static Caching: Disabled
Version: 5.53.0 PRO

Statamic Addons
anakadote/statamic-recaptcha: 1.0.18
rias/statamic-redirect: 3.10.5
statamic/eloquent-driver: 4.21.0
statamic/seo-pro: 6.6.3
thoughtco/statamic-restrict-fields: 1.1.0
transformstudios/review: 5.1.0

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

Installation

Fresh statamic/statamic site via CLI

Additional details

Worth noting that my Statamic installation was a fresh install of v4, into a Laravel 10 project. It has since been upgraded to Laravel 12 and Statamic 5.

@duncanmcclean
Copy link
Member
duncanmcclean commented Apr 22, 2025

Are you able to reproduce the issue on a blueprint without any fieldsets, or can you only reproduce it when fieldsets are involved?

Out of curiosity, does updating to 5.53.1 change anything? It made some changes to how replicator fields are validated.

@finnjsmith
Copy link
Author

Are you able to reproduce the issue on a blueprint without any fieldsets, or can you only reproduce it when fieldsets are involved?

Out of curiosity, does updating to 5.53.1 change anything? It made some changes to how replicator fields are validated.

@duncanmcclean I've tried whilst on 5.53.1, still no luck there: "This field is required unless link type is in collection.". I'm not sure what you mean about attempting to recreate without fieldsets as the issue is related to validating a fieldset's value within a fieldset. Do you mean outside of a bard field and just within a static blueprint?

@finnjsmith finnjsmith changed the title Fieldsets cannot validate against another fieldset's value. Fieldsets cannot validate against a nested fieldset's value. Apr 22, 2025
@duncanmcclean
Copy link
Member

I'm not sure what you mean about attempting to recreate without fieldsets as the issue is related to validating a fieldset's value within a fieldset. Do you mean outside of a bard field and just within a static blueprint?

Basically, setting up the blueprint in the same way (fields inside a Bard/Replicator, etc), but without using fieldsets.

@finnjsmith
Copy link
Author

I'm not sure what you mean about attempting to recreate without fieldsets as the issue is related to validating a fieldset's value within a fieldset. Do you mean outside of a bard field and just within a static blueprint?

Basically, setting up the blueprint in the same way (fields inside a Bard/Replicator, etc), but without using fieldsets.

Works exactly as expected and as I'd like it to - image is required at all times besides when link_type is equal to collection. It seems that the fieldsets are causing the issues. @duncanmcclean

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

No branches or pull requests

2 participants
0