Skip to content

Commit b6b1ca9

Browse files
authored
Merge pull request #375 from autogun/max_chars
Max chars input validation
2 parents 85f923c + 00c455e commit b6b1ca9

5 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/model/external_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def parameter_to_external(parameter):
4141
'type': parameter.type,
4242
'min': parameter.min,
4343
'max': parameter.max,
44+
'max_length': parameter.max_length,
4445
'values': parameter.values,
4546
'secure': parameter.secure,
4647
'fileRecursive': parameter.file_recursive,

src/model/parameter_config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'type',
2828
'min',
2929
'max',
30+
'max_length',
3031
'constant',
3132
'_values_provider',
3233
'values',
@@ -71,6 +72,7 @@ def _reload(self):
7172
self.required = read_bool_from_config('required', config, default=False)
7273
self.min = config.get('min')
7374
self.max = config.get('max')
75+
self.max_length = config.get('max_length')
7476
self.secure = read_bool_from_config('secure', config, default=False)
7577
self.separator = config.get('separator', ',')
7678
self.multiple_arguments = read_bool_from_config('multiple_arguments', config, default=False)
@@ -277,6 +279,9 @@ def validate_value(self, value, *, ignore_required=False):
277279
return None
278280

279281
if self.type == 'text':
282+
if (not is_empty(self.max_length)) and (len(value) > int(self.max_length)):
283+
return 'is longer than allowed char length (' \
284+
+ str(len(value)) + ' > ' + str(self.max_length) + ')'
280285
return None
281286

282287
if self.type == 'file_upload':
@@ -291,7 +296,7 @@ def validate_value(self, value, *, ignore_required=False):
291296
int_value = int(value)
292297

293298
if (not is_empty(self.max)) and (int_value > int(self.max)):
294-
return 'is greater than allowed value (' \
299+
return 'is longer than allowed value (' \
295300
+ value_string + ' > ' + str(self.max) + ')'
296301

297302
if (not is_empty(self.min)) and (int_value < int(self.min)):

web-src/src/admin/components/scripts-config/ParameterConfigForm.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
secure: 'secure',
135135
min: 'min',
136136
max: 'max',
137+
max_length: 'max_length',
137138
multipleArguments: 'multiple_arguments',
138139
sameArgParam: 'same_arg_param',
139140
separator: 'separator',
@@ -169,6 +170,7 @@
169170
description: null,
170171
min: null,
171172
max: null,
173+
max_length: null,
172174
allowedValues: null,
173175
allowedValuesScript: null,
174176
allowedValuesFromScript: null,
@@ -193,6 +195,7 @@
193195
descriptionField,
194196
minField,
195197
maxField: Object.assign({}, maxField),
198+
maxLengthField,
196199
allowedValuesScriptField,
197200
allowedValuesFromScriptField,
198201
defaultValueField: Object.assign({}, defaultValueField),
@@ -221,6 +224,7 @@
221224
this.required = get(config, 'required', false);
222225
this.min = config['min'];
223226
this.max = config['max'];
227+
this.max_length = config['max_length'];
224228
this.constant = !!get(config, 'constant', false);
225229
this.secure = !!get(config, 'secure', false);
226230
this.multipleArguments = !!get(config, 'multiple_arguments', false);

web-src/src/admin/components/scripts-config/parameter-fields.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export const allowedValuesScriptField = {
6767
required: true
6868
};
6969

70+
export const maxLengthField = {
71+
name: 'Max Characters',
72+
type: 'int'
73+
};
74+
7075
export const allowedValuesFromScriptField = {
7176
name: 'Load from script',
7277
withoutValue: true,

web-src/src/common/components/textfield.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
}
122122
123123
if (!empty) {
124-
var typeError = getValidByTypeError(value, this.config.type, this.config.min, this.config.max);
124+
var typeError = getValidByTypeError(value, this.config.type, this.config.min, this.config.max, this.config.max_length);
125125
if (!isEmptyString(typeError)) {
126126
return typeError;
127127
}
@@ -149,7 +149,15 @@
149149
}
150150
}
151151
152-
function getValidByTypeError(value, type, min, max) {
152+
function getValidByTypeError(value, type, min, max, max_length) {
153+
if (type === 'text') {
154+
if (max_length) {
155+
if (value.length > max_length) {
156+
return 'Max chars allowed: ' + max_length
157+
}
158+
}
159+
}
160+
153161
if (type === 'int') {
154162
const isInteger = /^(((-?[1-9])(\d*))|0)$/.test(value);
155163
if (!isInteger) {

0 commit comments

Comments
 (0)