Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: support to initial values for form templates through /new-to…
…pic (#23313) * FEATURE: adds support for initial values through /new-topic to form templates
- Loading branch information
Showing
27 changed files
with
178 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
app/assets/javascripts/discourse/app/components/form-template-field/checkbox.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/assets/javascripts/discourse/app/components/form-template-field/multi-select.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Component from "@glimmer/component"; | ||
import { action } from "@ember/object"; | ||
|
||
export default class FormTemplateFieldMultiSelect extends Component { | ||
@action | ||
isSelected(option) { | ||
return this.args.value?.includes(option); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
class FormTemplateYamlValidator < ActiveModel::Validator | ||
RESERVED_KEYWORDS = %w[title body category category_id tags] | ||
ALLOWED_TYPES = %w[checkbox dropdown input multi-select textarea upload] | ||
|
||
def validate(record) | ||
begin | ||
yaml = Psych.safe_load(record.template) | ||
check_missing_type(record, yaml) | ||
check_missing_fields(record, yaml) | ||
check_allowed_types(record, yaml) | ||
check_ids(record, yaml) | ||
rescue Psych::SyntaxError | ||
record.errors.add(:template, I18n.t("form_templates.errors.invalid_yaml")) | ||
end | ||
end | ||
|
||
def check_allowed_types(record, yaml) | ||
allowed_types = %w[checkbox dropdown input multi-select textarea upload] | ||
yaml.each do |field| | ||
if !allowed_types.include?(field["type"]) | ||
if !ALLOWED_TYPES.include?(field["type"]) | ||
return( | ||
record.errors.add( | ||
:template, | ||
I18n.t( | ||
"form_templates.errors.invalid_type", | ||
type: field["type"], | ||
valid_types: allowed_types.join(", "), | ||
valid_types: ALLOWED_TYPES.join(", "), | ||
), | ||
) | ||
) | ||
end | ||
end | ||
end | ||
|
||
def check_missing_type(record, yaml) | ||
def check_missing_fields(record, yaml) | ||
yaml.each do |field| | ||
if field["type"].blank? | ||
return record.errors.add(:template, I18n.t("form_templates.errors.missing_type")) | ||
return(record.errors.add(:template, I18n.t("form_templates.errors.missing_type"))) | ||
end | ||
if field["id"].blank? | ||
return(record.errors.add(:template, I18n.t("form_templates.errors.missing_id"))) | ||
end | ||
end | ||
end | ||
|
||
def check_ids(record, yaml) | ||
ids = [] | ||
yaml.each do |field| | ||
next if field["id"].blank? | ||
|
||
if RESERVED_KEYWORDS.include?(field["id"]) | ||
return( | ||
record.errors.add(:template, I18n.t("form_templates.errors.reserved_id", id: field["id"])) | ||
) | ||
end | ||
|
||
if ids.include?(field["id"]) | ||
return(record.errors.add(:template, I18n.t("form_templates.errors.duplicate_ids"))) | ||
end | ||
|
||
ids << field["id"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.