Override validation of x-data, based on content-type

Enonic version: 6.15.4
OS: Windows 10

I need to change the validation of my x-data, based on content type.
For example, I have this x-data:

<x-data>
  <display-name>My X-Data</display-name>
  <allowContentType>contentA</allowContentType>
  <allowContentType>contentB</allowContentType>
  <items>
    <input type="TextLine" name="myField">
      <label>My Field</label>
      <occurrences minimum="1" maximum="1"/>
    </input>
  ...
</x-data>

If the field “myField” is required in contentA, and not required in contentB, how I do this? Is possible?

You simply create two different X-data. If there are many similar fields use mixins!

1 Like

This is an idea, but I need to use the same x-data for all content-types. Because I use their name reference in my code.

There’s no way to have content-type based validation in x-data.
As Thomas mentioned, you could use a mixin inside content type schema. You could create two different mixins with the same set of fields but different values of occurrences (depending on which fields should be mandatory in which content types) and plug them in inside content type schemas.

You won’t have to care about x-data name since data from a mixin will simply be injected into content data as opposite to x-data that is stored separately inside “x/appName” node.

Note that in the example below the only difference between the two mixins is value in occurrences.minimum - for mixin A the field is mandatory, for mixin B it’s not.

mixin_for_content_type_A.xml:

<mixin>
<display-name>My field</display-name>
  <items>
    <input type="TextLine" name="myField">
      <label>Street address</label>
      <occurrences minimum="1" maximum="1"/>
    </input>
  </items>
</mixin>

mixin_for_content_type_B.xml:

<mixin>
<display-name>My field</display-name>
  <items>
    <input type="TextLine" name="myField">
      <label>Street address</label>
      <occurrences minimum="0" maximum="1"/>
    </input>
  </items>
</mixin>

content_type_A.xml:

<content-type>
  <display-name>Content type A</display-name>
  <super-type>base:structured</super-type>
  <form>
    <inline mixin="mixin_for_content_type_A"/>
  </form>
</content-type>

content_type_B.xml:

<content-type>
  <display-name>Content type B</display-name>
  <super-type>base:structured</super-type>
  <form>
    <inline mixin="mixin_for_content_type_B"/>
  </form>
</content-type>
1 Like

I understand now. It’s a good idea and solves my problem.
Thanks, guys!

1 Like

This topic was automatically closed after 4 hours. New replies are no longer allowed.