statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.08k stars 533 forks source link

Required sometimes triggering when it shouldn't #9334

Closed robdekort closed 9 months ago

robdekort commented 10 months ago

Bug description

I someimes see required sometimes triggering validation when it's hidden. Here's an example: https://github.com/statamic/cms/assets/69107412/0aa600a8-21e2-45ac-a86b-43d6ceb757d0

How to reproduce

This is a nested situation. It's probably easiest to:

  1. Install Peak
  2. Run php please add-block
  3. Add a block to the page builder.
  4. Use the following fieldset for this page builder block.
  5. Add the block to a page and trigger the validation error.
title: 'Columns'
fields:
  -
    handle: heading
    field:
      input_type: text
      type: text
      localizable: true
      listable: hidden
      display: Heading
  -
    handle: numbers
    field:
      inline_label: 'Render big numbers within the columns.'
      default: false
      type: toggle
      display: Numbers
      icon: toggle
      width: 50
      listable: hidden
      instructions_position: above
      visibility: visible
      replicator_preview: true
      hide_display: false
  -
    handle: number
    field:
      input_type: number
      antlers: false
      type: text
      display: Number
      icon: text
      instructions: 'Start counting from this number.'
      listable: hidden
      instructions_position: below
      visibility: visible
      replicator_preview: true
      hide_display: false
      width: 50
      validate:
        - sometimes
        - required
      if:
        numbers: 'equals true'
      always_save: true
  -
    handle: columns
    field:
      reorderable: true
      type: replicator
      localizable: true
      listable: hidden
      display: Columns
      replicator_preview: false
      instructions_position: above
      visibility: visible
      collapse: accordion
      previews: true
      fullscreen: true
      sets:
        columns:
          display: Columns
          instructions: null
          icon: null
          sets:
            column:
              display: Column
              instructions: null
              icon: null
              fields:
                -
                  handle: text
                  field: common.text_basic
                  config:
                    display: Text
                    validate: required
      hide_display: true

Logs

No response

Environment

Environment
Laravel Version: 10.40.0
PHP Version: 8.3.0
Composer Version: 2.5.5
Environment: local
Debug Mode: ENABLED
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

Locales
Installed
LaravelLang\Actions\Plugin: 1.4.1
LaravelLang\Attributes\Plugin: 2.9.1
LaravelLang\HttpStatuses\Plugin: 3.8.0
LaravelLang\Lang\Plugin: 13.12.0
Locales Version: 1.9.0
Protected
Publisher Version: 15.0.2

Livewire
Livewire: v3.3.5

Statamic
Addons: 7
Antlers: runtime
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.42.1 PRO

Statamic Addons
jacksleight/statamic-bard-mutator: 2.3.0
jonassiewertsen/statamic-livewire: 3.0.0
rias/statamic-color-swatches: 2.2.0
studio1902/statamic-peak-browser-appearance: 3.3.4
studio1902/statamic-peak-commands: 4.0.0
studio1902/statamic-peak-seo: 8.0.0
studio1902/statamic-peak-tools: 4.2.0

Installation

Starter Kit using via CLI

Antlers Parser

Runtime (default)

Additional details

No response

robdekort commented 10 months ago

A workaround is using 'required_if:{this}.numbers,true' as the validation rule.

jasonvarga commented 9 months ago

This is working as intended.

You have the number field set to always_save: true, which means it will always be submitted. Any submitted field will pass the sometimes rule, and therefore your required rule kicks in.

You can either disable always_save, or use the required_if validation rule like you found out.

robdekort commented 9 months ago

Oh Wow I would've never guessed that. Makes sense-ish though. Thanks for the explanation.