Hello,

I'm trying to figure out how to create a condition involving a negation. Eg. Enable current field IF previous field value IS NOT something. I think it's not implemented yet, at a field level.

Thank you!

CommentFileSizeAuthor
#4 add_support_for-2858142-4.patch827 bytesjrockowitz

Comments

mariuscmr created an issue. See original summary.

jrockowitz’s picture

Please provide a concrete example with a code snippet.

mariuscmr’s picture

It's about cities in Romania.

I have a city autocomplete field, a county field (Select2) and Sector (Select2).

Bucharest does not belong to a county, but it's splitted into sectors (1 to 6).

The first field to fill is City.

In normal conditions, county field is required and sector optional.

The logic would be this:

If people fill in "Bucharest", disable County and enable Sector.
AND
Make County optional and Sector required.

If people fill in OTHER but Bucharest, leave county enabled and sector disabled
AND
Make County required and Sector optional.

If I try to make county required by default, when people fill in Bucharest, I the form throws an "Illegal choice" error.
Do I have to start will county and sector optional and make them required depending on the conditionals?

The snippet you asked me for is working but not in my desired way (county and sector still remain optional, regardless of city choice).

I will paste here only that part, not the whole form.

Thanks.

Translation for values in the form are:

Localitate = City
Judet = County
Sector is the same

  layout_destinatar_4:
    '#type': flexbox
    '#align_items': flex-end
    prbe_destinatar_localitate:
      '#type': autocomplete
      '#title': Localitate
      '#flex': '4'
      '#attributes':
        class:
          - form-control
      '#required': true
      '#required_error': 'Trebuie sa alegi o localitate'
      '#autocomplete_items': localitati_romania
      '#autocomplete_match': '2'
      '#autocomplete_match_operator': STARTS_WITH
    prbe_destinatar_judet:
      '#type': select
      '#title': Judet
      '#flex': '2'
      '#options': judete_romania
      '#empty_option': 'Alege un judet'
      '#empty_value': 'Fara judet'
      '#select2': true
      '#states':
        disabled:
          ':input[name="prbe_destinatar_localitate"]':
            value: Bucuresti
        optional:
          ':input[name="prbe_destinatar_localitate"]':
            value: Bucuresti
        required:
          ':input[name="prbe_destinatar_localitate"]':
            value: true
    prbe_destinatar_sector:
      '#type': select
      '#title': Sector
      '#flex': '2'
      '#options':
        1: '1'
        2: '2'
        3: '3'
        4: '4'
        5: '5'
        6: '6'
      '#empty_option': 'Alege sectorul'
      '#empty_value': '-'
      '#select2': true
      '#required_error': 'Trebuie sa completezi sectorul'
      '#states':
        enabled:
          ':input[name="prbe_destinatar_localitate"]':
            value: Bucuresti
        required:
          ':input[name="prbe_destinatar_localitate"]':
            value: Bucuresti
    prbe_destinatar_cp:
      '#type': textfield
      '#title': 'Cod Postal'
      '#autocomplete': 'off'
      '#input_mask': '999999'
      '#flex': '2'
    prbe_destinatar_op:
      '#type': textfield
      '#title': 'Oficiul Postal'
      '#placeholder': 'Ex. Suceava 3'
      '#flex': '2'
jrockowitz’s picture

Status: Active » Needs review
StatusFileSize
new827 bytes

Your use case is very complex.

The below optional and required states are definitely conflicting.

    '#states':
        disabled:
          ':input[name="prbe_destinatar_localitate"]':
            value: Bucuresti
        optional:
          ':input[name="prbe_destinatar_localitate"]':
            value: Bucuresti
        required:
          ':input[name="prbe_destinatar_localitate"]':
            value: true

You have convinced me to add the 'Value is not' (!value) trigger to the conditional logic UI.

  • jrockowitz committed be4b936 on 8.x-5.x
    Issue #2858142 by jrockowitz: Add support for negative conditions
    
jrockowitz’s picture

Status: Needs review » Fixed

I committed the patch. Please download and review the latest dev release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.