1
0
Fork 0

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'), showPrefill: or('isText', 'isCategory', 'isTag', 'isGroup', 'isDropdown'),
showContent: or('isCategory', 'isTag', 'isGroup', 'isDropdown'), showContent: or('isCategory', 'isTag', 'isGroup', 'isDropdown'),
showLimit: or('isCategory', 'isTag'), showLimit: or('isCategory', 'isTag'),
showMinLength: or('isText', 'isTextarea', 'isComposer'), showLengthControls: or('isText', 'isTextarea', 'isComposer'),
categoryPropertyTypes: selectKitContent(['id', 'slug']), categoryPropertyTypes: selectKitContent(['id', 'slug']),
showAdvanced: alias('field.type'), showAdvanced: alias('field.type'),
messageUrl: 'https://thepavilion.io/t/2809', messageUrl: 'https://thepavilion.io/t/2809',

Datei anzeigen

@ -8,7 +8,7 @@ import { popupAjaxError } from 'discourse/lib/ajax-error';
const CustomWizard = EmberObject.extend({ const CustomWizard = EmberObject.extend({
save(opts) { save(opts) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let wizard = this.buildJson(this, 'wizard'); let wizard = this.buildJson(this, 'wizard');
if (wizard.error) { if (wizard.error) {

Datei anzeigen

@ -70,7 +70,7 @@
url=messageUrl url=messageUrl
component='field'}} component='field'}}
{{#if showMinLength}} {{#if showLengthControls}}
<div class="setting"> <div class="setting">
<div class="setting-label"> <div class="setting-label">
<label>{{i18n 'admin.wizard.field.min_length'}}</label> <label>{{i18n 'admin.wizard.field.min_length'}}</label>
@ -84,6 +84,20 @@
class="small"}} class="small"}}
</div> </div>
</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}}
{{#if isUpload}} {{#if isUpload}}

Datei anzeigen

@ -164,6 +164,8 @@ en:
required_label: "Field is Required" required_label: "Field is Required"
min_length: "Min Length" min_length: "Min Length"
min_length_placeholder: "Minimum length in characters" min_length_placeholder: "Minimum length in characters"
max_length: "Max Length"
max_length_placeholder: "Maximum length in characters"
file_types: "File Types" file_types: "File Types"
limit: "Limit" limit: "Limit"
property: "Property" property: "Property"

Datei anzeigen

@ -20,6 +20,7 @@ en:
field: field:
too_short: "%{label} must be at least %{min} characters" too_short: "%{label} must be at least %{min} characters"
too_long: "%{label} must not be more than %{max} characters"
required: "%{label} is required." required: "%{label} is required."
not_url: "%{label} must be a valid url" not_url: "%{label} must be a valid url"
invalid_file: "%{label} must be a %{types}" invalid_file: "%{label} must be a %{types}"

Datei anzeigen

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

Datei anzeigen

@ -5,6 +5,7 @@ module CustomWizardFieldExtension
:image, :image,
:key, :key,
:min_length, :min_length,
:max_length,
:file_types, :file_types,
:format, :format,
:limit, :limit,
@ -18,6 +19,7 @@ module CustomWizardFieldExtension
@image = attrs[:image] @image = attrs[:image]
@key = attrs[:key] @key = attrs[:key]
@min_length = attrs[:min_length] @min_length = attrs[:min_length]
@max_length = attrs[:max_length]
@file_types = attrs[:file_types] @file_types = attrs[:file_types]
@format = attrs[:format] @format = attrs[:format]
@limit = attrs[:limit] @limit = attrs[:limit]

Datei anzeigen

@ -158,6 +158,7 @@ class CustomWizard::Builder
params[:image] = field_template['image'] if field_template['image'] params[:image] = field_template['image'] if field_template['image']
params[:key] = field_template['key'] if field_template['key'] params[:key] = field_template['key'] if field_template['key']
params[:min_length] = field_template['min_length'] if field_template['min_length'] 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) params[:value] = prefill_field(field_template, step_template)
if !build_opts[:reset] && (submission = @wizard.current_submission) if !build_opts[:reset] && (submission = @wizard.current_submission)

Datei anzeigen

@ -3,14 +3,17 @@ class CustomWizard::Field
@types ||= { @types ||= {
text: { text: {
min_length: nil, min_length: nil,
max_length:nil,
prefill: nil prefill: nil
}, },
textarea: { textarea: {
min_length: nil, min_length: nil,
max_length:nil,
prefill: nil prefill: nil
}, },
composer: { composer: {
min_length: nil min_length: nil,
max_length:nil,
}, },
text_only: {}, text_only: {},
date: { date: {

Datei anzeigen

@ -21,6 +21,7 @@ class ::CustomWizard::UpdateValidator
type = field.type type = field.type
required = field.required required = field.required
min_length = field.min_length if is_text_type(field) 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 file_types = field.file_types
format = field.format 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)) @updater.errors.add(field_id, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
end 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) if is_url_type(field) && !check_if_url(value)
@updater.errors.add(field_id, I18n.t('wizard.field.not_url', label: label)) @updater.errors.add(field_id, I18n.t('wizard.field.not_url', label: label))
end end

Datei anzeigen

@ -49,7 +49,32 @@ describe CustomWizard::UpdateValidator do
updater.errors.messages[:step_1_field_3].first updater.errors.messages[:step_1_field_3].first
).to eq(I18n.t('wizard.field.too_short', label: 'Composer', min: min_length)) ).to eq(I18n.t('wizard.field.too_short', label: 'Composer', min: min_length))
end 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 it 'standardises boolean entries' do
updater = perform_validation('step_2', step_2_field_5: 'false') updater = perform_validation('step_2', step_2_field_5: 'false')
expect(updater.submission['step_2_field_5']).to eq(false) expect(updater.submission['step_2_field_5']).to eq(false)