diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 7615fe07..b17464d4 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -21,7 +21,7 @@ export default Component.extend(UndoChanges, { showPrefill: or('isText', 'isCategory', 'isTag', 'isGroup', 'isDropdown'), showContent: or('isCategory', 'isTag', 'isGroup', 'isDropdown'), showLimit: or('isCategory', 'isTag'), - showMinLength: or('isText', 'isTextarea', 'isComposer'), + showLengthControls: or('isText', 'isTextarea', 'isComposer'), categoryPropertyTypes: selectKitContent(['id', 'slug']), showAdvanced: alias('field.type'), messageUrl: 'https://thepavilion.io/t/2809', diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs index 8e673247..2792fdc5 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -70,7 +70,7 @@ url=messageUrl component='field'}} -{{#if showMinLength}} +{{#if showLengthControls}}
@@ -84,6 +84,20 @@ class="small"}}
+ +
+
+ +
+ +
+ {{input + type="number" + name="max_length" + value=field.max_length + class="small"}} +
+
{{/if}} {{#if isUpload}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 5bd88f24..be138d48 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -164,6 +164,8 @@ en: required_label: "Field is Required" min_length: "Min Length" min_length_placeholder: "Minimum length in characters" + max_length: "Max Length" + max_length_placeholder: "Maximum length in characters" file_types: "File Types" limit: "Limit" property: "Property" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index d6c136ff..4bda825a 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -20,6 +20,7 @@ en: field: too_short: "%{label} must be at least %{min} characters" + too_long: "%{label} must not be more than %{max} characters" required: "%{label} is required." not_url: "%{label} must be a valid url" invalid_file: "%{label} must be a %{types}" diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index 9a0dc4cf..a9a01035 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -97,6 +97,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController :key, :type, :min_length, + :max_length, :file_types, :format, :limit, diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 375f0421..6e843e2c 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "covered_percent": 89.41 + "covered_percent": 89.45 } } diff --git a/extensions/wizard_field.rb b/extensions/wizard_field.rb index 2301c6d3..9fd8d6d1 100644 --- a/extensions/wizard_field.rb +++ b/extensions/wizard_field.rb @@ -5,6 +5,7 @@ module CustomWizardFieldExtension :image, :key, :min_length, + :max_length, :file_types, :format, :limit, @@ -18,6 +19,7 @@ module CustomWizardFieldExtension @image = attrs[:image] @key = attrs[:key] @min_length = attrs[:min_length] + @max_length = attrs[:max_length] @file_types = attrs[:file_types] @format = attrs[:format] @limit = attrs[:limit] diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index e530e5e9..d7d5a517 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -158,6 +158,7 @@ class CustomWizard::Builder params[:image] = field_template['image'] if field_template['image'] params[:key] = field_template['key'] if field_template['key'] params[:min_length] = field_template['min_length'] if field_template['min_length'] + params[:max_length] = field_template['max_length'] if field_template['max_length'] params[:value] = prefill_field(field_template, step_template) if !build_opts[:reset] && (submission = @wizard.current_submission) diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index cbb08e9c..53a8b5da 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -3,14 +3,17 @@ class CustomWizard::Field @types ||= { text: { min_length: nil, + max_length: nil, prefill: nil }, textarea: { min_length: nil, + max_length: nil, prefill: nil }, composer: { - min_length: nil + min_length: nil, + max_length: nil, }, text_only: {}, date: { diff --git a/lib/custom_wizard/validators/update.rb b/lib/custom_wizard/validators/update.rb index 0229f676..0db83c01 100644 --- a/lib/custom_wizard/validators/update.rb +++ b/lib/custom_wizard/validators/update.rb @@ -21,6 +21,7 @@ class ::CustomWizard::UpdateValidator type = field.type required = field.required min_length = field.min_length if is_text_type(field) + max_length = field.max_length if is_text_type(field) file_types = field.file_types format = field.format @@ -32,6 +33,10 @@ class ::CustomWizard::UpdateValidator @updater.errors.add(field_id, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i)) end + if max_length && value.is_a?(String) && value.strip.length > max_length.to_i + @updater.errors.add(field_id, I18n.t('wizard.field.too_long', label: label, max: max_length.to_i)) + end + if is_url_type(field) && !check_if_url(value) @updater.errors.add(field_id, I18n.t('wizard.field.not_url', label: label)) end diff --git a/spec/components/custom_wizard/update_validator_spec.rb b/spec/components/custom_wizard/update_validator_spec.rb index 3bceb939..996c1548 100644 --- a/spec/components/custom_wizard/update_validator_spec.rb +++ b/spec/components/custom_wizard/update_validator_spec.rb @@ -49,7 +49,57 @@ describe CustomWizard::UpdateValidator do updater.errors.messages[:step_1_field_3].first ).to eq(I18n.t('wizard.field.too_short', label: 'Composer', min: min_length)) end - + + it 'prevents submission if the length is over the max length' do + max_length = 100 + + @template[:steps][0][:fields][0][:max_length] = max_length + @template[:steps][0][:fields][1][:max_length] = max_length + @template[:steps][0][:fields][2][:max_length] = max_length + + CustomWizard::Template.save(@template) + long_string = "Our Competitive Capability solution offers platforms a suite of wholesale offerings. In the future, will you be able to effectively revolutionize synergies in your business? In the emerging market space, industry is ethically investing its mission critical executive searches. Key players will take ownership of their capabilities by iteratively right-sizing world-class visibilities. " + updater = perform_validation('step_1', step_1_field_1: long_string) + expect( + updater.errors.messages[:step_1_field_1].first + ).to eq(I18n.t('wizard.field.too_long', label: 'Text', max: max_length)) + + updater = perform_validation('step_1', step_1_field_2: long_string) + expect( + updater.errors.messages[:step_1_field_2].first + ).to eq(I18n.t('wizard.field.too_long', label: 'Textarea', max: max_length)) + + updater = perform_validation('step_1', step_1_field_3: long_string) + expect( + updater.errors.messages[:step_1_field_3].first + ).to eq(I18n.t('wizard.field.too_long', label: 'Composer', max: max_length)) + end + + it "allows submission if the length is under or equal to the max length" do + max_length = 100 + + @template[:steps][0][:fields][0][:max_length] = max_length + @template[:steps][0][:fields][1][:max_length] = max_length + @template[:steps][0][:fields][2][:max_length] = max_length + + CustomWizard::Template.save(@template) + hundred_chars_string = "This is a line, exactly hundred characters long and not more even a single character more than that." + updater = perform_validation('step_1', step_1_field_1: hundred_chars_string) + expect( + updater.errors.messages[:step_1_field_1].first + ).to eq(nil) + + updater = perform_validation('step_1', step_1_field_2: hundred_chars_string) + expect( + updater.errors.messages[:step_1_field_2].first + ).to eq(nil) + + updater = perform_validation('step_1', step_1_field_3: hundred_chars_string) + expect( + updater.errors.messages[:step_1_field_3].first + ).to eq(nil) + end + it 'standardises boolean entries' do updater = perform_validation('step_2', step_2_field_5: 'false') expect(updater.submission['step_2_field_5']).to eq(false)