statamic / cms

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

Validation of hidden conditional (required) field prevents saving an entry #1495

Closed sauerbraten closed 4 years ago

sauerbraten commented 4 years ago

We have a section of a blueprint like this:

-
  handle: paywall_inactive
  field:
    type: toggle
    display: Inactive
-
  handle: paywall_inactive_from
  field:
    type: date
    display: 'Inactive from'
    time_enabled: true
    required: true
    show_when:
      paywall_inactive: true
-
  handle: paywall_inactive_until
  field:
    type: date
    display: 'Inactive until'
    time_enabled: true
    required: true
    show_when:
      paywall_inactive: true

When we leave the paywall_inactive toggle off, the 'from' and 'until' fields aren't shown, so a user can't set them. When saving the entry, validation fails because of the required: true setting in the two fields.

I guess those should only be validated when their show_when condition is met?

jasonvarga commented 4 years ago

Try using validate: 'required_if:paywall_inactive' instead of required: true (which is an alias for validate: required)

sauerbraten commented 4 years ago

Oh, thank you! validate: 'required_if:paywall_inactive,true' fixed it (the true at the end was necessary).

jasonvarga commented 4 years ago

Ah there we go!

stuartcusackie commented 1 year ago

It would be nice if conditions on hidden fields were not applied, i.e. if a field is not shown then don't apply it's validation rules.

edalzell commented 1 year ago

It would be nice if conditions on hidden fields were not applied, i.e. if a field is not shown then don't apply it's validation rules.

Please add this to the ideas repo @stuartcusackie

jasonvarga commented 1 year ago

Hidden fields are not submitted, so as long as they don't have a required rule, it wont validate the rest of the rules on the field.

stuartcusackie commented 1 year ago

I see... it's just a little confusing. It makes sense from a Laravel perspective, i.e. required fields are always required, but from a Statamic blueprint perspective it just doesn't feel intuitive, i.e. why would we ever require a hidden field.

Perhaps there's something that can be done to remove 'required' rules automatically when the field is not visible. Maybe the required Toggle could behave differently than an explicitly typed 'required' rule.

Anyway, I know now and hopefully I'll remember this in future when my entries won't save.

jesseleite commented 1 year ago

@stuartcusackie This is what the sometimes rule is for.

With sometimes|required, laravel/statamic will only apply the required rule if it exists in the request (ie. if the field is shown via conditions). We document this here...

CleanShot 2023-01-25 at 19 22 05

Source: https://statamic.dev/conditional-fields#validation

stuartcusackie commented 1 year ago

Thanks Jesse. I do understand this. I was trying to make the point that simply using Laravel validation rules doesn't feel intuitive to me in the context of the blueprint builder and maybe Statamic should automatically and intelligently handle situations like this. As I said, I don't ever see a case where we would want a hidden field to be required so maybe Statamic should prevent me from making this mistake.

Other form builders I have used tend to ignore all validations when a field is conditionally hidden, including 'required' conditions. It's unclear what Nova's stance on it is but somebody else seems to think the same way as me on this.

jesseleite commented 1 year ago

Maybe we could get more clever down the road with the UI in the validation tab of the blueprint builder, but transparency around laravel's documented validation ruleset is by design, and very important; Certain rules would behave much differently than documented if the user didn't realize we were magically removing required or applying sometimes without them knowing it. Until then, we dedicate a whole validation section in the conditional fields documentation to this 😊

If you wish to conditionally apply validation to conditionally shown fields, we recommend using the sometimes Laravel validation rule.

jesseleite commented 1 year ago

@sauerbraten @stuartcusackie We've added a sometimes rule toggle to help expose this functionality in the validation builder. See the PR here... https://github.com/statamic/cms/pull/7416 👍