From f2d7f57bb65fa83c93ffef77cfde0ede659a508a Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Mon, 23 Mar 2020 14:49:39 +0000 Subject: [PATCH 1/2] first feature commit --- assets/javascripts/wizard/controllers/custom-step.js.es6 | 1 + .../wizard/templates/components/wizard-field-number.hbs | 1 + lib/custom_wizard/builder.rb | 2 ++ lib/custom_wizard/field.rb | 2 +- 4 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 assets/javascripts/wizard/templates/components/wizard-field-number.hbs diff --git a/assets/javascripts/wizard/controllers/custom-step.js.es6 b/assets/javascripts/wizard/controllers/custom-step.js.es6 index 1881764c..892f785b 100644 --- a/assets/javascripts/wizard/controllers/custom-step.js.es6 +++ b/assets/javascripts/wizard/controllers/custom-step.js.es6 @@ -4,6 +4,7 @@ import getUrl from 'discourse-common/lib/get-url'; export default StepController.extend({ actions: { goNext(response) { + debugger; const next = this.get('step.next'); if (response.redirect_on_next) { window.location.href = response.redirect_on_next; diff --git a/assets/javascripts/wizard/templates/components/wizard-field-number.hbs b/assets/javascripts/wizard/templates/components/wizard-field-number.hbs new file mode 100644 index 00000000..33fae047 --- /dev/null +++ b/assets/javascripts/wizard/templates/components/wizard-field-number.hbs @@ -0,0 +1 @@ +{{input type='number' step='0.01' id=field.id value=field.value}} \ No newline at end of file diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index ea8bdc01..7298e9ac 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -330,6 +330,7 @@ class CustomWizard::Builder end def validate_field(field, updater, step_template) + byebug value = updater.fields[field['id']] min_length = false label = field['label'] || I18n.t("#{field['key']}.label") @@ -356,6 +357,7 @@ class CustomWizard::Builder validator[:block].call(field, updater, step_template) end end + byebug end def is_text_type(field) diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index c68379a6..2afc3237 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -1,6 +1,6 @@ class CustomWizard::Field def self.types - @types ||= ['checkbox', 'composer', 'dropdown', 'tag', 'category', 'image', 'text', 'textarea', 'text-only', 'upload', 'user-selector'] + @types ||= ['checkbox', 'composer', 'dropdown', 'tag', 'category', 'image', 'text', 'textarea', 'text-only', 'number', 'upload', 'user-selector'] end def self.require_assets From ce9373dd911d377060c6a739fece200a1ac6d48a Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Mon, 23 Mar 2020 18:40:11 +0000 Subject: [PATCH 2/2] working url and number validation --- .../components/wizard-custom-field.js.es6 | 2 +- .../wizard/controllers/custom-step.js.es6 | 1 - .../wizard/initializers/custom.js.es6 | 5 ++++- .../templates/components/wizard-field-url.hbs | 1 + config/locales/server.en.yml | 1 + lib/custom_wizard/builder.rb | 18 ++++++++++++++++-- lib/custom_wizard/field.rb | 2 +- 7 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 assets/javascripts/wizard/templates/components/wizard-field-url.hbs diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index abd9f459..66bcd014 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -14,7 +14,7 @@ export default Ember.Component.extend({ categoryPropertyTypes: generateSelectKitContent(['id', 'slug']), @computed('field.type') - isInput: (type) => type === 'text' || type === 'textarea', + isInput: (type) => type === 'text' || type === 'textarea' || type === 'url', @computed('field.type') isCategoryOrTag: (type) => type === 'tag' || type === 'category', diff --git a/assets/javascripts/wizard/controllers/custom-step.js.es6 b/assets/javascripts/wizard/controllers/custom-step.js.es6 index 892f785b..1881764c 100644 --- a/assets/javascripts/wizard/controllers/custom-step.js.es6 +++ b/assets/javascripts/wizard/controllers/custom-step.js.es6 @@ -4,7 +4,6 @@ import getUrl from 'discourse-common/lib/get-url'; export default StepController.extend({ actions: { goNext(response) { - debugger; const next = this.get('step.next'); if (response.redirect_on_next) { window.location.href = response.redirect_on_next; diff --git a/assets/javascripts/wizard/initializers/custom.js.es6 b/assets/javascripts/wizard/initializers/custom.js.es6 index de336fa7..5797116e 100644 --- a/assets/javascripts/wizard/initializers/custom.js.es6 +++ b/assets/javascripts/wizard/initializers/custom.js.es6 @@ -214,13 +214,14 @@ export default { inputComponentName: function() { const type = this.get('field.type'); const id = this.get('field.id'); - if (type === 'text-only') return false; + if (['text-only'].includes(type)) return false; return (type === 'component') ? Ember.String.dasherize(id) : `wizard-field-${type}`; }.property('field.type', 'field.id') }); const StandardFieldValidation = [ 'text', + 'number', 'textarea', 'dropdown', 'tag', @@ -259,6 +260,8 @@ export default { valid = val && val.id > 0; } else if (StandardFieldValidation.indexOf(type) > -1) { valid = val && val.length > 0; + } else if (type === 'url') { + valid = true } } diff --git a/assets/javascripts/wizard/templates/components/wizard-field-url.hbs b/assets/javascripts/wizard/templates/components/wizard-field-url.hbs new file mode 100644 index 00000000..2c3844af --- /dev/null +++ b/assets/javascripts/wizard/templates/components/wizard-field-url.hbs @@ -0,0 +1 @@ +{{input type='text' id=field.id value=field.value}} \ No newline at end of file diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 8033e4a7..fe3554a3 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -9,6 +9,7 @@ en: field: too_short: "%{label} must be at least %{min} characters" required: "%{label} is required." + not_url: "%{label} must be a valid url" none: "We couldn't find a wizard at that address." no_skip: "Wizard can't be skipped" export: diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index e7f03a66..ba163513 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -1,3 +1,5 @@ +require 'uri' + TagStruct = Struct.new(:id, :name) class CustomWizard::Builder @@ -330,7 +332,6 @@ class CustomWizard::Builder end def validate_field(field, updater, step_template) - byebug value = updater.fields[field['id']] min_length = false label = field['label'] || I18n.t("#{field['key']}.label") @@ -347,6 +348,12 @@ class CustomWizard::Builder updater.errors.add(field['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i)) end + if is_url_type(field) + if !check_if_url(value) + updater.errors.add(field['id'].to_s, I18n.t('wizard.field.not_url', label: label)) + end + end + ## ensure all checkboxes are booleans if field['type'] === 'checkbox' updater.fields[field['id']] = standardise_boolean(value) @@ -357,13 +364,20 @@ class CustomWizard::Builder validator[:block].call(field, updater, step_template) end end - byebug end def is_text_type(field) ['text', 'textarea'].include? field['type'] end + def is_url_type(field) + ['url'].include? field['type'] + end + + def check_if_url(value) + value =~ URI::regexp + end + def standardise_boolean(value) ActiveRecord::Type::Boolean.new.cast(value) end diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index 2afc3237..859e47fa 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -1,6 +1,6 @@ class CustomWizard::Field def self.types - @types ||= ['checkbox', 'composer', 'dropdown', 'tag', 'category', 'image', 'text', 'textarea', 'text-only', 'number', 'upload', 'user-selector'] + @types ||= ['checkbox', 'composer', 'dropdown', 'tag', 'category', 'image', 'text', 'textarea', 'text-only', 'number', 'upload', 'user-selector', 'url'] end def self.require_assets