0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-09-19 23:31:11 +02:00

FEATURE: added max length setting for text type fields

Dieser Commit ist enthalten in:
Faizaan Gagan 2020-12-08 12:44:37 +05:30
Ursprung be204c1cb6
Commit 4f7d18d193
11 geänderte Dateien mit 59 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -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',

Datei anzeigen

@ -70,7 +70,7 @@
url=messageUrl
component='field'}}
{{#if showMinLength}}
{{#if showLengthControls}}
<div class="setting">
<div class="setting-label">
<label>{{i18n 'admin.wizard.field.min_length'}}</label>
@ -84,6 +84,20 @@
class="small"}}
</div>
</div>
<div class="setting full">
<div class="setting-label">
<label>{{i18n 'admin.wizard.field.max_length'}}</label>
</div>
<div class="setting-value">
{{input
type="number"
name="max_length"
value=field.max_length
class="small"}}
</div>
</div>
{{/if}}
{{#if isUpload}}

Datei anzeigen

@ -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"

Datei anzeigen

@ -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}"

Datei anzeigen

@ -97,6 +97,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController
:key,
:type,
:min_length,
:max_length,
:file_types,
:format,
:limit,

Datei anzeigen

@ -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]

Datei anzeigen

@ -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)

Datei anzeigen

@ -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: {

Datei anzeigen

@ -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

Datei anzeigen

@ -50,6 +50,31 @@ describe CustomWizard::UpdateValidator do
).to eq(I18n.t('wizard.field.too_short', label: 'Composer', min: min_length))
end
it 'applies max length to text type fields' 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 'standardises boolean entries' do
updater = perform_validation('step_2', step_2_field_5: 'false')
expect(updater.submission['step_2_field_5']).to eq(false)