Hi,

The Form state "Emptied" is not working for the "Target field" when the "Controlled field" condition is applied!

USE CASE:

Target Field: Text Box (filled in with some text)
Controlled Field: Boolean field
The target field is: Emptied
When the controlled field: Is Checked

When you check the controlled field (Boolean), the Target field (Text Box) IS NOT EMPTIED.

Any help please ?

Thank you,

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

C.E.A created an issue. See original summary.

c.e.a’s picture

Any update on this issue please!

snehalgaikwad’s picture

Starting work on patch for this.

c.e.a’s picture

thank you for your time, waiting for the patch

c.e.a’s picture

@snehalgaikwad thank you for your support, I would like to ask if there is a working in progress for the patch ?
If you have free time available of course

snehalgaikwad’s picture

Assigned: Unassigned » snehalgaikwad
markdc’s picture

I'm having a similar problem with a select box not being emptied.

phonkala’s picture

Any progress with this? Same problem here and I noticed there is quite similar issue too: https://www.drupal.org/project/conditional_fields/issues/2918601

In my case I have a select list "Sign-Up Type" with multiple options, including "Built-In Webform" and "Custom Webform". I also have 2 additional select lists "Built-In Webforms" and "Custom Webforms" that both include a list of webforms specific for each sign-up type. This way user can choose which type of sign-up to use (in their events) and the webform lists are hidden accordingly, so if user chooses sign-up type of "Custom Webform", the "Built-In Webforms" list is hidden. That works perfectly.

The problem is that when doing exactly the same thing but with the intention to actually "empty" the other webform list, it just does nothing. For example again, if the user has chosen sign-up type of "Custom Webform", the "Built-In Webforms" select list should be hidden (works) but also be "emptied" (does not work). Using "Filled with a Value" and manually entering desired value does nothing either.

I tested both the "Filled with a value" and "Emptied" actions with multiple sets of different types of target and source fields, even plain text fields and none worked at all. Making fields visible/invisible with exactly same conditions worked every time. Hope this helps even a bit, the issue seems pretty major to me as it's a huge part of the idea of conditional fields functionality. Sadly I'm not experienced enough with Drupal 8 development I could be much of help here.

abhisekmazumdar’s picture

Hi, @snehalgaikwad I'm Picking this up. If there any update from your side can let me know.
Thank You

abhisekmazumdar’s picture

Status: Active » Needs review
StatusFileSize
new1.71 KB

Hi,
The field gets emptied now, but the value is not getting preserved. I would need some suggestion about what should be the approach for preserving the value.
For now, I have added a patch with proper validation if Target Filed is required or not.

nikunjkotecha’s picture

Hi Abhisek,

We shouldn't change the text used in t() if not required, this change would required update in translations that are already added in a site using this module.

Please justify the need of this change or revert it.

-          $form_state->setErrorByName('state', $this->t('Field !field is required and can not have state !state.', [
-            '!field' => $field_instance->getLabel() . ' (' . $field_instance->getName() . ')',
-            '!state' => $all_states[$state],
+          $form_state->setErrorByName('state', $this->t('Field %field is required and can not have state %state.', [
+            '%field' => $field_instance->getLabel() . ' (' . $field_instance->getName() . ')',
+            '%state' => $all_states[$state],
nikunjkotecha’s picture

Status: Needs review » Needs work
abhisekmazumdar’s picture

Status: Needs work » Needs review
StatusFileSize
new1.37 KB
new948 bytes

Thanks, @nikunjkotecha for the inputs.
The validation for the setErrorByName will be fixed on Invalid placeholder (!field). So I have removed it from my patch.

nikunjkotecha’s picture

Patch looks good to me.

abhisekmazumdar’s picture

StatusFileSize
new1.76 KB
new878 bytes

Updated the patch, now it Restores previous value.

abhisekmazumdar’s picture

Assigned: abhisekmazumdar » Unassigned
divya.sejekan’s picture

Assigned: Unassigned » divya.sejekan
divya.sejekan’s picture

Assigned: divya.sejekan » Unassigned
Status: Needs review » Reviewed & tested by the community

I have tested the patch. And the issue is resolved.

divya.sejekan’s picture

phonkala’s picture

Hey there!

Haven't had the possibility to test the patch yet but checked the code.

I would believe using only .val() function won't solve the problem in every case: as far as I know, it only works when the element can have value attribute and the value shown is based on that (like input fields).

You propably need to use some .attr("selected") function for select elements, .attr("checked") function for radio buttons / checkboxes etc. Or if I remember correctly, in jQuery you can also use .toggle() in these cases.

Also, textarea doesn't use value attribute to show the contents of the element but the content is written between <textarea> tags, not sure though if in jQuery it can be set using .val(). But as said, haven't been able to test this, just a notice and something to test.

playful’s picture

I tried #15 with autocomplete and text fields as the targets with a checkbox as controller. The target field is successfully emptied, however the target field also emptied each time the node edit form page is loaded.

It does not work with address field as target.

playful’s picture

#15 also introduces some other strange behavior. To reproduce, set two text fields as targets to be invisible when a boolean checkbox controller field is unchecked. In the form, each time the controller is unchecked, only one of the target fields becomes invisible. The checkbox must be checked and unchecked multiple times to iterate through each target field.

Seems like a simple misplacement of a foreach/while loop.

abhisekmazumdar’s picture

Status: Reviewed & tested by the community » Needs work

The last patch needs work. Anyone can pick this up I have some other priority work.

schnydszch’s picture

Hi! I am also experiencing this. I'm on Drupal 8.9.11. Thanks in advance!

Yara Emmanuelle’s picture

Hey there!
This patch also doesn't work for Conditional fields 4.0@alpha version.

Yara Emmanuelle’s picture

Hey there!
This patch also doesn't work for Conditional fields 4.0.0-alpha1 version.

alexander.levitsky made their first commit to this issue’s fork.

saurabh-2k17’s picture

StatusFileSize
new784 bytes

Hi, attaching a working patch.

  • saurabh-2k17 authored 34321a8d on 4.x
    Issue #3022750 by saurabh-2k17: Form State "Emptied" is not working for...
szhu’s picture

Status: Needs work » Reviewed & tested by the community
saurabh-2k17’s picture

Status: Reviewed & tested by the community » Fixed

Thank you everyone for your efforts.

Status: Fixed » Closed (fixed)

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