Empty action sets field value to "undefined"

Created on 17 February 2025, about 2 months ago

Problem/Motivation

When I set a conditional field, which should empty the field value when the condition is triggered.
When the condition is triggered the field value becomes 'undefined'

Proposed resolution

This patch seems to work, looks perhaps this is a simple bug.

diff --git a/js/conditional_fields.js b/js/conditional_fields.js
index 61e0bbd..104be6a 100644
--- a/js/conditional_fields.js
+++ b/js/conditional_fields.js
@@ -68,7 +68,7 @@
             $(this).data('conditionalFieldsSavedValue', this.value);
           }
           if (e.effect) {
-            if (e.value) {
+            if (e.effect.value) {
               this.value = e.effect.value;
             } else if ($(this).data('conditionalFieldsSavedValue')) {
               this.value = $(this).data('conditionalFieldsSavedValue');

πŸ› Bug report
Status

Active

Version

4.0

Component

Javascript

Created by

πŸ‡ΊπŸ‡ΈUnited States markusa

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @markusa
  • πŸ‡ΊπŸ‡ΈUnited States markusa

    Actually the first patch doesn't work, it doesn't empty the fields. I trying this

    diff --git a/js/conditional_fields.js b/js/conditional_fields.js
    index 61e0bbd..b73d9be 100644
    --- a/js/conditional_fields.js
    +++ b/js/conditional_fields.js
    @@ -70,6 +70,9 @@
               if (e.effect) {
                 if (e.value) {
                   this.value = e.effect.value;
    +              if (typeof e.effect.value === 'undefined') {
    +                this.value = '';
    +              }
                 } else if ($(this).data('conditionalFieldsSavedValue')) {
                   this.value = $(this).data('conditionalFieldsSavedValue');
                 }
    
    

    Probably a better/different way to solve this maybe module maintainers know.

  • πŸ‡ΊπŸ‡¦Ukraine knyshuk.vova

    The reason why we have this issue:

    4.0.0-alpha5
    $(this).val(e.effect.value);
    val(undefined) in jQuery sets the value to an empty string (""), not string "undefined".

    4.0.0-alpha6
    this.value = e.effect.value;
    e.effect.value is undefined, the value property is set to string "undefined"

  • πŸ‡ΊπŸ‡¦Ukraine knyshuk.vova

    Diff from previous patch:

    • replaced if with nullish coalescing operator
    • added some comments.
Production build 0.71.5 2024