Conditional validation

Last updated on
21 August 2023

Conditional validation is an available option to select after you choose a validator. As the name implies, if it is selected (and configured correctly) the validation rule will only be ran if the condition is true.

There are three (unlabeled) fields under the header Condition field:

  1. condition_field
  2. condition_operator
  3. condition_value

They work as one would expect; the validation rule is ignored if the statement evaluates as false. This can be used to define validation rules for a given field based upon the contents of a different field.

In the first field, you need to specify the token of the field you wish to check. The UI does not give you the available tokens for this field.

A valid token matches the following pattern (among others):

[entity-type:field-name:field-column]

So, for example, to specify a term id of a term reference field named "field_manufacturer," you might have a token like this:

[node:field-manufacturer:tid]
[node:field-manufacturer:tid] equals 22

For the term name of the same field:

[node:field-manufacturer:name]
[node:field-manufacturer:name] not equals Samsung

The entity bundle is not a part of this token, but the entity type is. "node" is the most common entity and "user" is another core entity type.

So, for an example of a field validation rule with conditional validation, the following will accept only a legit phone number on a phone support field if the manufacturer taxonomy term is not Samsung (and, presumably, something that is not a legit phone number when the term ref field is Samsung):

  • Your validator is "Phone" on a field labeled "phone support"
  • Your conditional validation is [node:field-manufacturer:name] not equals Samsung - the operator is "not equals" and "Samsung" is the value

If the term is "Apple" (and apple is not samsung) the conditional validation is true, the validator will run, and the field labeled phone support will demand a legit phone.

If you have the advanced help module enabled you can get a list of tokens here: /admin/help/token

Note: I found this feature request helpful - https://www.drupal.org/project/field_validation/issues/2979254

Help improve this page

Page status: Not set

You can: