diff options
| -rw-r--r-- | app/javascript/helpers/CustomFieldsInputs.js | 4 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 19 | ||||
| -rw-r--r-- | spec/models/custom_field_spec.rb | 2 |
3 files changed, 19 insertions, 6 deletions
diff --git a/app/javascript/helpers/CustomFieldsInputs.js b/app/javascript/helpers/CustomFieldsInputs.js index 93611538e..5240a9649 100644 --- a/app/javascript/helpers/CustomFieldsInputs.js +++ b/app/javascript/helpers/CustomFieldsInputs.js @@ -18,9 +18,11 @@ export default class CustomFieldsInputs extends Component { } listInput(cf){ + let keys = _.orderBy(_.keys(this.options(cf).list_values), function(i){ return parseInt(i)}) return( <Select2 - data={_.map(this.options(cf).list_values, (v, k) => { + data={_.map(keys, (k) => { + let v = this.options(cf).list_values[k] return {id: k, text: (v.length > 0 ? v : '\u00A0')} })} ref={'custom_fields.' + cf.code} diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index deb0326f8..7fb45ffa0 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -158,12 +158,23 @@ class CustomField < ApplicationModel end class List < Integer + def collection_is_a_hash? + options["list_values"].is_a?(Hash) + end + def validate - super return unless value.present? - unless value >= 0 && value < options["list_values"].size - @owner.errors.add errors_key, "'#{@raw_value}' is not a valid value" - @valid = false + if collection_is_a_hash? + unless options["list_values"].include(value.to_s) + @owner.errors.add errors_key, "'#{@raw_value}' is not a valid value" + @valid = false + end + else + super + unless value >= 0 && value < options["list_values"].size + @owner.errors.add errors_key, "'#{@raw_value}' is not a valid value" + @valid = false + end end end diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index 0c2644499..5fbd19719 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -104,7 +104,7 @@ RSpec.describe CustomField, type: :model do if valid expect(vj.validate).to be_truthy else - expect(vj.validate).to be_falsy + expect(vj.validate).to be_falsy, "'#{val}' should not be valid value" expect(vj.errors.messages[:"custom_fields.energy"]).to be_present end end |
