0

Ok. Everyone's got an answer on how to add validation rules to a ui-component field via XML. Unfortunately, I can't find anyone that seems to know how to implement a rule that requires a parameter or two. I'm sure the knowledge is out there, but nobody's asked the question. So here it is.

I'm looking to implement the min-words validation rule for a textarea. I've got something like this:

<field name="text_area">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            ...
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
                <item name="min-words" xsi:type="array">
                    <item name="length" xsi:type="number">10</item>
                </item>
            </item>
            ...
        </item>
    </argument>
</field>

but I'm guessing, and this doesn't quite work. I'm close, but can't figure out what the "length" parameter is really supposed to be, and looking at the rules.js file doesn't provide any clues.

Toby Crain
  • 111
  • 2
  • 4

1 Answers1

0

So for the min-words rule, I found that it's simply:

<item name="min-words" xsi:type="number">10</item>

Having seen other implementations of parameters (not in validation rules), I thought I'd try this for the range-words rule, and discovered this format:

<item name="range-words" xsi:type="array">
   <item name="0" xsi:type="number">5</item>
   <item name="1" xsi:type="number">10</item>
</item>

This format would serve similar rules that require parameters, such as zip-range, validate-number-range, validate-digits-range, validate-range, validate-date-range, validate-item-quantity, validate-file-type. Being mindful to use the name="0" as array index for each entry.

Toby Crain
  • 111
  • 2
  • 4