-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[5.4] Fix: Showon fails open when source field does not exist (hidden by default instead) #46488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 5.4-dev
Are you sure you want to change the base?
[5.4] Fix: Showon fails open when source field does not exist (hidden by default instead) #46488
Conversation
|
Does this respect the [AND] and [OR] Operators? |
|
Yes, when the second condition is not applicable, the field is hidden. ( I described it in the test instructions) |
|
1. Test Using Custom Fields Add two lines in "List Values" like blow List values | Text* | Value | This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/46488. |
|
@ThomasFinnern changed, thank you - did you test it? |
|
I tried to get there but failed due to lack of my test knowledge. |
|
I have tested this item ✅ successfully on 45133c8 The above test cases in *.xml worked before the patch and afterwards. This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/46488. |
|
Hello @coolcat-creations This PR "hides" a miss-configuration of the system, so we thought that the site owner should be informed about the issue in some way, either here in the JS code with a console log message or in the backend inside the field, that the showOn is broken (or both, but happy with one). Could you please add this somehow? Thank you in advance. |
|
That's a good idea too. So instead of showing the field, I will write into console "hidden because showon condition is invalid" ? is that ok? @bembelimen |
|
Yes, your code + the extra comment |
|
@bembelimen ok, done - can someone retest? |
|
shouldnt that warning be translatable? |
|
Somehow, information seems to have been lost along the way to @bembelimen . We agreed in the maintainer team that a warning would definitely be useful. The potential problem here arises at the latest when showon is used with a negative condition. Then, unfortunately, the code may have exactly the opposite effect to what you want to achieve and lead to even more confusion. In the core itself, it should probably remain a simple warning and dispatching the event so that even less experienced users or developers can quickly become aware of the problem and recognise their misconfiguration or bug in their xml. However, if you keep the event in there, you can then add an Eventlistener and hide it in according to your specific needs. |
| field.classList.add('hidden'); | ||
| field.dispatchEvent(new CustomEvent('joomla:showon-hide', { | ||
| bubbles: true, | ||
| })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| field.classList.add('hidden'); | |
| field.dispatchEvent(new CustomEvent('joomla:showon-hide', { | |
| bubbles: true, | |
| })); | |
| field.dispatchEvent(new CustomEvent('joomla:showon-hide', { | |
| detail: {reason: 'missingParent'} | |
| bubbles: true, | |
| })); |
Just a suggestion.
Related comment: #46488 (comment)
What do you think, @Fedik @dgrammatiko would it make sense to have a separate event here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep it minimal. Without extra events.
If you really need to have some flag about missing parent then can do kind of:
field.dispatchEvent(new CustomEvent('joomla:showon-hide', {
detail: {missingParent: true}
bubbles: true,
}));Or:
detail: {reason: 'missingParent'}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice thanks for the suggestion.
You would then leave field.classList.add(“hidden”); in place?
Even if that means that in a condition such as, for example, foo!: or foo!:1 are we more likely risk the undesirable side effect of data loss until the user notices it?
Otherwise, I find the event name somewhat confusing.
I don't think it will ever fit in any direction anyway. The main question is in which direction it could potentially cause more problems. 🙃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personalty I would not hide the field, only show the warning about misconfiguration.
But if people want we can hide.
There should not be any data lose because it just visual thing and nothing to do with field values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wasn't sure if I had understood you correctly, but that basically confirms what we discussed earlier.
|
Let’s use an example: If Branches is deleted, doesn’t exist, or is unpublished, the cuisines field is currently still shown. That’s incorrect, because the showon condition can’t be met in that case. The cuisines field should definitely be hidden. |
|
Yes, it really isn't easy, I understand what you mean. I'll give it a try with your example. showon = branches:!retail Then you may eventually realise that retail simply does not suit your target group and delete the Branches field completely because you no longer need the information. Then the cuisine field would never be shown, even though there is a greater potential here that it should be displayed. And by the time you notice, you have potential "data loss" because you will not fill in a field that you cannot see. What we are actually missing in the custom field case is validation at the field configuration level in the backend to catch the underlying issue. But I don't see that within the scope of this PR. |
This is misconfiguration can lead to even more confusion. For example you have 50+ fields. After some time you delete Branches field and the cuisine field become always hidden. You forget cuisine field even exists because it is not visible. This is argument not to hide the field because it will hide bugs in User's setup. |
The change only hides when the trigger field itself isn’t found no input/select named branches (or branches[]) was For your example showon="branches:!retail" specifically:
|
how would you know then the setup is invalid... ? I think Warnings in the console could be really helpful. |
Yes, absolutely, and that's why we're very grateful to you for your improvement. The only thing we don't want is what Fedik already mentioned.
|
|
However, since the event you originally implemented is still there, you can use this event for example, in your template The core should not mask issues or misconfiguration and thus make debugging more difficult. |

This pull request fixes an issue in Joomla’s showon implementation where dependent fields are always shown, even when the referenced source field does not exist in the DOM.
This “fail-open” behaviour can lead to incorrect form states, invalid UI logic, and unintended visibility of fields.
Problem
Currently, if a field contains something like:
showon="nonexistent_field:1"and nonexistent_field is not present in the form:
This is counter-intuitive and causes issues especially in:
This behaviour is likely an oversight rather than an intentional feature.
Expected Behaviour
If the source field defined in showon does not exist, then the condition cannot be fulfilled, therefore the dependent field should be hidden by default.
Solution
This PR adds a simple and safe check in Showon (build/media_source/system/js/showon.es6.js):
After collecting all origin fields for a given showon condition:
if origin.length === 0,
→ the dependent field is immediately hidden,
→ and the joomla:showon-hide event is fired.
This changes the behaviour to Fail closed instead of fail open
(a missing condition hides a field rather than incorrectly showing it)
The fix does not modify the data structure, PHP FormHelper logic, or any API and is fully backward compatible unless someone relied on the previous incorrect behaviour (unlikely).
How to Test
Please test both Custom Fields and standard XML JForm definitions.
1. Test Using Custom Fields
A) Prepare the fields
Go to:
Content → Fields
Create a field:
Field A:
Type: List
Name: controller
Options
List values | Text* | Value |
___________ | Yes _ | 1 |
___________ | No _ | 0 |
Create another field:
Field B:
Type: Text
Name: dependent
Showon:
controller:1B) Test the normal behaviour
Edit an article.
Set “Controller” to "Yes" → Field B should appear.
Set “Controller” to "No" → Field B should hide.
C) Test the missing-source behaviour
Unpublish or Delete Field A (the controller field)
Open the article again.
Expected result after applying the PR:
“Dependent” field is hidden by default
No JS errors occur
Switching values of other fields does not accidentally reveal it
Current behaviour (before PR):
Field B is incorrectly visible.
2. Test Using XML Form Definitions
A) Create a simple XML form (administrator or component)
Example snippet:
<field name="dependent" type="text" label="Dependent Field" showon="nonexistent:1" />B) Load the form in the backend (module, component, plugin etc.)
Expected result after PR:
The “dependent” field is hidden because the source field does not exist.
Current behaviour:
The field is incorrectly shown.
showon="controller:1[AND]nonexistent:2"Expected after PR:
Even if controller exists, the missing nonexistent should invalidate the entire condition, hiding the field.
Backward Compatibility
This change should not break valid configurations.
The only difference occurs when showon references a field that:
does not exist,
was renamed,
was deleted,
or is dynamically removed in subforms.
In these cases the new behaviour is more correct and prevents accidental visibility.
This PR makes the showon behaviour more predictable, safer, and consistent by hiding dependent fields when the source field is missing — a long-standing edge case that often leads to incorrect UI states.