Closed sauerbraten closed 4 years ago
Try using validate: 'required_if:paywall_inactive'
instead of required: true
(which is an alias for validate: required
)
Oh, thank you! validate: 'required_if:paywall_inactive,true'
fixed it (the true
at the end was necessary).
Ah there we go!
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.
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
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.
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.
@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...
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.
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.
@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 👍
We have a section of a blueprint like this:
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 therequired: true
setting in the two fields.I guess those should only be validated when their
show_when
condition is met?