Skip to content

A platform for community discussion. Free, open, simple.

FIX: Embeddings settings shows model selected when it is not (#42403)

Natalie Taye509833fe063
diff --git a/frontend/discourse/app/components/setting-field/enum.gjs b/frontend/discourse/app/components/setting-field/enum.gjs
index 1e215755b15..be7ebbd2c50 100644
--- a/frontend/discourse/app/components/setting-field/enum.gjs
+++ b/frontend/discourse/app/components/setting-field/enum.gjs
@@ -34,12 +34,24 @@ export default class SettingFieldEnum extends Component {
return String(this.args.field.value ?? "");
}
+ get hasInvalidSelection() {
+ return (
+ this.selectedValue !== "" &&
+ !this.choices.some((choice) => choice.value === this.selectedValue)
+ );
+ }
+
<template>
<@field.Control
@includeNone={{this.includeNone}}
@nonePlaceholder={{if this.includeNone (i18n "admin.settings.none")}}
as |select|
>
+ {{#if this.hasInvalidSelection}}
+ <option value={{this.selectedValue}} selected disabled>
+ {{i18n "admin.settings.none"}}
+ </option>
+ {{/if}}
{{#each this.choices as |choice|}}
<select.Option @value={{choice.value}} @selected={{this.selectedValue}}>
{{choice.name}}
diff --git a/frontend/discourse/tests/integration/components/setting-definition-field-test.gjs b/frontend/discourse/tests/integration/components/setting-definition-field-test.gjs
index b54895c097e..4969aa3ccdf 100644
--- a/frontend/discourse/tests/integration/components/setting-definition-field-test.gjs
+++ b/frontend/discourse/tests/integration/components/setting-definition-field-test.gjs
@@ -6,6 +6,7 @@ import SettingDefinitionField from "discourse/components/setting-definition-fiel
import Site from "discourse/models/site";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import selectKit from "discourse/tests/helpers/select-kit-helper";
+import { i18n } from "discourse-i18n";
module("Integration | Component | SettingDefinitionField", function (hooks) {
setupRenderingTest(hooks);
@@ -378,6 +379,41 @@ module("Integration | Component | SettingDefinitionField", function (hooks) {
assert.form().field("my_enum").hasValue("a");
});
+ test("enum does not select the first choice when the current value is invalid", async function (assert) {
+ await render(
+ <template>
+ <Form @data={{hash my_enum="missing"}} as |form|>
+ <SettingDefinitionField
+ @definition={{hash
+ key="my_enum"
+ type="enum"
+ label="My enum"
+ valid_values=(array
+ (hash value="a" name="Apple") (hash value="b" name="Banana")
+ )
+ }}
+ @form={{form}}
+ />
+ </Form>
+ </template>
+ );
+
+ assert
+ .dom("[data-name='my_enum'] select option[value='missing']")
+ .hasText(
+ i18n("admin.settings.none"),
+ "the invalid value is presented as no selection"
+ )
+ .hasAttribute("disabled", "", "the invalid option cannot be selected");
+ assert
+ .form()
+ .field("my_enum")
+ .hasValue(
+ "missing",
+ "the browser does not select the first valid choice"
+ );
+ });
+
test("enum only offers a none option when the setting allows a blank value", async function (assert) {
await render(
<template>