statamic / cms

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

Sometimes required validation doesn't work with forms conditional logic #9172

Open robdekort opened 9 months ago

robdekort commented 9 months ago

Bug description

I've found that validating something as required sometimes doesn't always work. If it doesn't it triggers a required field validation even when the field isn't visible. Using required_if instead is a way to work around this.

I've ran into this on multiple occasions, but didn't jot them down and I can only replicate the issue in Forms today. I'll report the other scenarios as soon as I run into them again.

https://github.com/statamic/cms/assets/69107412/88f14902-a4db-404f-a7ac-7521c4cb46c9

In this clip the I'm conditional field shouldn't trigger a validation error when the name field isn't Rob.

How to reproduce

  1. Use conditional logic on a form field.
  2. Set that form field to required sometimes.
  3. Try to submit the form and get a required validation error even when the conditional field is hidden.

Logs

No response

Environment

Environment
Application Name: Statamic Peak Development
Laravel Version: 10.35.0
PHP Version: 8.2.10
Composer Version: 2.5.8
Environment: local
Debug Mode: ENABLED
URL: statamic-peak.test
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: 4
Antlers: runtime
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.39.0 PRO

Statamic Addons
studio1902/statamic-peak-browser-appearance: dev-main
studio1902/statamic-peak-commands: 3.1.0
studio1902/statamic-peak-seo: 7.3.2
studio1902/statamic-peak-tools: 4.0.3

Installation

Starter Kit using via CLI

Antlers Parser

None

Additional details

No response

robdekort commented 9 months ago

I think the other times where sometimes doesn't work might have to do with imported fieldsets and using prefixes. Which is a separate issue. I'll try to narrow this down and report another one.

jasonvarga commented 9 months ago

Are you able to reproduce this in an entry (since you can't create form submissions in the cp) and see if the same thing happens in the control panel publish form?

robdekort commented 9 months ago

Hey Jason, I couldn't replicate this specific issue in the backend. It seems to work fine there.

jasonvarga commented 9 months ago

This is the form with precognition, right?

My guess is that the $form is initialized with all the data, so it gets submitted.

The conditional logic needs to be able to remove it from that object when the field is hidden. Neat.

robdekort commented 9 months ago

Yeah that sounds like it. I used to run into this when I submitted with Ajax myself as well btw.

finnjsmith commented 1 month ago

I'm encountering this bug when using forms in a Navigation blueprint in the backend. The fields are hiding / showing conditionally as expected, but sometimes doesn't appear to be respected and is causing every field to be validated even when a lot aren't shown. I'm not using any fieldsets or prefixes as mentioned above. I can provide more details if necessary, but my Statamic version is 5.19, and I'm using Eloquent Driver 4.12 for storing entries on MacOS.

Here's a link to the discord thread which details the issues I was having.

godismyjudge95 commented 3 weeks ago

This is also happening with the user creation wizard and custom fields that are required + sometimes and conditionally hidden. Link to discord