Cms: Array validation syntax doesn't work inside Replicator etc

Created on 17 Apr 2020  路  11Comments  路  Source: statamic/cms

Bug Description

There's no great way to validate against fields in the same context/set inside replicator.

How to Reproduce

-
  handle: replicator
  field:
    type: replicator
    sets:
      intro:
        fields:
          -
            handle: add_cta
            field:
              type: toggle
          -
            handle: cta_text
            field:
              type: text
              validate: make this required if add_cta in the same set is true        

You might try validate: 'require_if:replicator.*.add_cta,true' but the * doesn't get replaced as expected.

Extra Detail

Replicator already duplicates the validation rules for all the sets.
If you have validate: somerule on a foo field, it actually duplicates it for as many times as that set was used in the submitted data.

Lets say you submitted a replicator field and the first and fourth sets had that foo field, it would generate these rules:

replicator.0.foo => 'somerule',
replicator.3.foo => 'somerule',

I think the solution should be to allow an asterisk to represent "this field and this index".

validate: 'require_if:*.add_cta,true'

or maybe leave the asterisk to mean the index, and have a separate thing as a placeholder for the field:

validate: 'require_if:{parent}.*.add_cta,true'

Should generate something like:

replicator.0.foo => 'required_if:replicator.0.add_cta',
replicator.3.foo => 'required_if:replicator.3.add_cta',

You shouldn't need to put the name of the field you're in. It may cause issues if you're using importing that field from a fieldset etc.

Environment

Statamic version: 3.0.0.-beta.26

enhancement fieldtypes

All 11 comments

Settled on using {this} as a placeholder for the parent and index.

validate: 'require_if:{this}.add_cta,true'

@jasonvarga thanks for creating this issue, I did a little more digging and found that the context of the validation wasn't being communicated in the validation process:

"page_builder.0.cta_text" => [
    "required_if:page_builder.*.add_cta,1"
]

By the looks of things there needs to be some way of ensuring that the relative index is included in the rule. {this}.add_cta looks ideal!

Would this also apply to conditional logic? And if so, how would the syntax in the YAML file look?

if:
   - '{this}.thing,something'

You can use #2289 for an example where nested validation doesn't work. It's contains an example using the {this} syntax if you need to test.

Would this also apply to conditional logic?

@robdekort, Nope. Unlike validation, field conditions always check current field level context, unless you prepend root to access top-level fields (see docs on field context).

Yep, makes sense now!

Our team is hoping this gets fixed soon as well

Can I confirm this is still an issue? The only reason I'm asking is I think I saw validation working in the Peak starter kit and it was doing exactly what I am trying to do. I have a simple replicator that has a bard in it. The sets for both the replicator and the bard in the replicator are imported fieldsets. The issue is the simple "required" validation on the bard sets doesn't work. Says something is required when it's filled and I try to save. Only way to make imported bard sets work is nothing required.

Yep. Still an issue.

Thanks Rob. Hold on though, in Peak I see you have a replicator (page builder) with a set for article that is a bard. Then a bard set in there for button. How is this required validation working? I tried the same and no deal.

  validate:
    - 'required_if:{this}.link_type,entry'

BTW: Thank you so much for Peak. I am learning A TON just going over what you did!

Hi Tom. Good to hear, super nice. Tbh I'm not sure anymore under what circumstances validation works. I remember {this}. or {root}. is the proposed new syntax that isn't available yet atm. Sorry, can't help you further for now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

filipac picture filipac  路  4Comments

andrewying picture andrewying  路  4Comments

riasvdv picture riasvdv  路  4Comments

aerni picture aerni  路  3Comments

wm-simon picture wm-simon  路  3Comments