From 801f84318a981a0935c2fd6e501985b30bdacc33 Mon Sep 17 00:00:00 2001 From: Manuel Wegria Date: Tue, 30 Apr 2024 14:07:10 +0200 Subject: [PATCH 1/8] fix date time and complex datation validations --- app/models/field/complex_datation.rb | 16 ++++++++++++---- app/models/field/date_time.rb | 26 ++++++++++++++++++++++++++ config/locales/app/fr.yml | 4 ++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/app/models/field/complex_datation.rb b/app/models/field/complex_datation.rb index 495e1c98c..ced10b36f 100644 --- a/app/models/field/complex_datation.rb +++ b/app/models/field/complex_datation.rb @@ -276,18 +276,26 @@ class ComplexDatationValidator < ActiveModel::Validator def validate(record) attrib = Array.wrap(options[:attributes]).first value = record.public_send(attrib) + field = Field.find_by(uuid: attrib) return if value.blank? - return if value['selected_format'] != "date_time" + return if value['to'].keys.all? { |key| value['to'][key].blank? || value['to'][key].nil? } && value['from'].keys.all? { |key| value['from'][key].blank? || value['from'][key].nil? } && !field.required - from_date_is_positive = value['from'].compact.except("BC").all? { |_, v| v.to_i >= 0 } + if value['to'].keys.all? { |key| value['to'][key].blank? || value['to'][key].nil? } && value['from'].keys.all? { |key| value['from'][key].blank? || value['from'][key].nil? } && field.required + record.errors.add(:base, I18n.t('activerecord.errors.models.item.attributes.base.complex_datation_cant_be_blank')) + return + end + from_date_is_positive = value['from'].compact.except("BC").all? { |_, v| v.to_i >= 0 } to_date_is_positive = value['to'].compact.except("BC").all? { |_, v| v.to_i >= 0 } - return if to_date_is_positive && from_date_is_positive + invalid_format = field.format.chars.any? do |char| + value['to'][char].blank? || value['to'][char].nil? || value['from'][char].blank? || value['from'][char].nil? + end - record.errors.add(:base, :negative_dates) + record.errors.add(:base, I18n.t('activerecord.errors.models.item.attributes.base.wrong_complex_datation_format', field_format: field.format)) if invalid_format + record.errors.add(:base, :negative_dates) if !to_date_is_positive || !from_date_is_positive end end end diff --git a/app/models/field/date_time.rb b/app/models/field/date_time.rb index 38f9541fa..9a1b9f45e 100644 --- a/app/models/field/date_time.rb +++ b/app/models/field/date_time.rb @@ -182,4 +182,30 @@ def coerce_to_array(values) array << values[key] end.compact end + + def build_validators + [DateTimeValidator] + end + + class DateTimeValidator < ActiveModel::Validator + def validate(record) + attrib = Array.wrap(options[:attributes]).first + value = record.public_send(attrib) + field = Field.find_by(uuid: attrib) + + return if value.blank? + return if value.keys.all? { |key| value[key].blank? || value[key].nil? } && !field.required + + if value.keys.all? { |key| value[key].blank? || value[key].nil? } && field.required + record.errors.add(:base, I18n.t('activerecord.errors.models.item.attributes.base.date_time_cant_be_blank')) + return + end + + invalid_format = field.format.chars.any? do |char| + value[char].blank? || value[char].nil? + end + + record.errors.add(:base, I18n.t('activerecord.errors.models.item.attributes.base.wrong_date_time_format', field_format: field.format)) if invalid_format + end + end end diff --git a/config/locales/app/fr.yml b/config/locales/app/fr.yml index 259b7699d..24214cc63 100644 --- a/config/locales/app/fr.yml +++ b/config/locales/app/fr.yml @@ -67,7 +67,11 @@ fr: item: attributes: base: + complex_datation_cant_be_blank: "Le champ complexe datation doit etre rempli" + date_time_cant_be_blank: "Le champ date time doit etre rempli" negative_dates: "Les dates négatives doivent être saisies avec l'option avant Jesus Christ (si activée)" + wrong_complex_datation_format: "Ne respecte pas le format %{field_format} du champ complex datation" + wrong_date_time_format: "Ne respecte pas le format %{field_format} du champ date time" field/choice_set: attributes: choice_set_id: From 11e90ceab871591a0de57632b354e671e9122a56 Mon Sep 17 00:00:00 2001 From: Manuel Wegria Date: Wed, 1 May 2024 16:12:52 +0200 Subject: [PATCH 2/8] move errors from base to attrib and display them on their correct place --- app/models/field/complex_datation.rb | 6 +++--- app/models/field/date_time.rb | 5 +++-- app/views/catalog_admin/items/_common_form_fields.html.erb | 5 +++++ config/locales/app/en.yml | 4 +++- config/locales/app/fr.yml | 6 ++---- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/models/field/complex_datation.rb b/app/models/field/complex_datation.rb index ced10b36f..162b7913e 100644 --- a/app/models/field/complex_datation.rb +++ b/app/models/field/complex_datation.rb @@ -283,7 +283,7 @@ def validate(record) return if value['to'].keys.all? { |key| value['to'][key].blank? || value['to'][key].nil? } && value['from'].keys.all? { |key| value['from'][key].blank? || value['from'][key].nil? } && !field.required if value['to'].keys.all? { |key| value['to'][key].blank? || value['to'][key].nil? } && value['from'].keys.all? { |key| value['from'][key].blank? || value['from'][key].nil? } && field.required - record.errors.add(:base, I18n.t('activerecord.errors.models.item.attributes.base.complex_datation_cant_be_blank')) + record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.cant_be_blank')) return end @@ -294,8 +294,8 @@ def validate(record) value['to'][char].blank? || value['to'][char].nil? || value['from'][char].blank? || value['from'][char].nil? end - record.errors.add(:base, I18n.t('activerecord.errors.models.item.attributes.base.wrong_complex_datation_format', field_format: field.format)) if invalid_format - record.errors.add(:base, :negative_dates) if !to_date_is_positive || !from_date_is_positive + record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: field.format)) if invalid_format + record.errors.add(attrib, :negative_dates) if !to_date_is_positive || !from_date_is_positive end end end diff --git a/app/models/field/date_time.rb b/app/models/field/date_time.rb index 9a1b9f45e..aaa47b98e 100644 --- a/app/models/field/date_time.rb +++ b/app/models/field/date_time.rb @@ -197,7 +197,7 @@ def validate(record) return if value.keys.all? { |key| value[key].blank? || value[key].nil? } && !field.required if value.keys.all? { |key| value[key].blank? || value[key].nil? } && field.required - record.errors.add(:base, I18n.t('activerecord.errors.models.item.attributes.base.date_time_cant_be_blank')) + record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.cant_be_blank')) return end @@ -205,7 +205,8 @@ def validate(record) value[char].blank? || value[char].nil? end - record.errors.add(:base, I18n.t('activerecord.errors.models.item.attributes.base.wrong_date_time_format', field_format: field.format)) if invalid_format + record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: field.format)) if invalid_format end end end + diff --git a/app/views/catalog_admin/items/_common_form_fields.html.erb b/app/views/catalog_admin/items/_common_form_fields.html.erb index 0c39e7436..07e35c800 100644 --- a/app/views/catalog_admin/items/_common_form_fields.html.erb +++ b/app/views/catalog_admin/items/_common_form_fields.html.erb @@ -13,6 +13,11 @@ :current_user => @current_user, :catalog => @catalog ) %> + <% if field.editor_component.present? %> + <% @item.errors.where(field.uuid).each do |error| %> +
<%= error.message %>
+ <% end %> + <% end %> <% end %>
diff --git a/config/locales/app/en.yml b/config/locales/app/en.yml index f4e703f97..7b40949fa 100644 --- a/config/locales/app/en.yml +++ b/config/locales/app/en.yml @@ -65,7 +65,9 @@ en: item: attributes: base: - negative_dates: "Negative dates must be entered with the option before Jesus Christ (if activated)" + cant_be_blank: "Doit etre rempli" + negative_dates: "Les dates négatives doivent être saisies avec l'option avant Jesus Christ (si activée)" + wrong_format: "Ne respecte pas le format %{field_format}" field/choice_set: attributes: choice_set_id: diff --git a/config/locales/app/fr.yml b/config/locales/app/fr.yml index 24214cc63..dd6836aac 100644 --- a/config/locales/app/fr.yml +++ b/config/locales/app/fr.yml @@ -67,11 +67,9 @@ fr: item: attributes: base: - complex_datation_cant_be_blank: "Le champ complexe datation doit etre rempli" - date_time_cant_be_blank: "Le champ date time doit etre rempli" + cant_be_blank: "Doit etre rempli" negative_dates: "Les dates négatives doivent être saisies avec l'option avant Jesus Christ (si activée)" - wrong_complex_datation_format: "Ne respecte pas le format %{field_format} du champ complex datation" - wrong_date_time_format: "Ne respecte pas le format %{field_format} du champ date time" + wrong_format: "Ne respecte pas le format %{field_format}" field/choice_set: attributes: choice_set_id: From a41adfb4c57d7177d5221e172b1aedcfedaf1e21 Mon Sep 17 00:00:00 2001 From: Manuel Wegria Date: Mon, 6 May 2024 10:52:14 +0200 Subject: [PATCH 3/8] move errors into react components --- .../components/ComplexDatationInput.jsx | 23 ++++++++++--------- .../components/DateTimeInput.jsx | 9 ++++---- app/models/field/complex_datation.rb | 6 +++++ app/models/field/date_time.rb | 11 +++++++++ .../items/_common_form_fields.html.erb | 5 ---- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx b/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx index b5a94d6bc..313ce36c1 100644 --- a/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx +++ b/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx @@ -16,7 +16,8 @@ const ComplexDatationInput = (props) => { choiceSets: choiceSetsProps, selectedFormat: selectedFormatProps, fieldUuid, - componentPolicies + componentPolicies, + errors } = props const [choiceSets, setChoiceSets] = useState(choiceSetsProps) @@ -353,21 +354,21 @@ const ComplexDatationInput = (props) => { ) } - if (!fromState || !toState) return "" + if (!fromState || !toState) return "" return (
{renderAllowedFormatsSelector()}
{selectedFormat === 'date_time' && ( -
-
{renderDateTimeInput('from')}
-
{renderDateTimeInput('to')}
- {errorMsg} -
+
+
{renderDateTimeInput('from', input)}
+
{renderDateTimeInput('to', input)}
+
{errors?.filter(e => e.field === input.split("#item_")[1].split("_json")[0])?.map(e => e.message)?.join(',')}
+
)} - {selectedFormat === 'datation_choice' && ( - { const { input, allowBC, - preventNegativeInput + preventNegativeInput, + errors } = props const [state, setState] = useState(false) @@ -39,7 +40,6 @@ const DateTimeInput = (props) => { let dateValid = isCurrentFormatValid(); let errorStl = dateValid ? {} : {border: "2px solid #f00"}; - let errorMsg = dateValid ? "" : "Invalid value"; let fmt = getFieldOptions().format; function _handleChangeDay(e) { @@ -218,7 +218,8 @@ const DateTimeInput = (props) => { ) : null } {fmt.includes('Y') ? ( - ) : null } @@ -238,7 +239,7 @@ const DateTimeInput = (props) => { ) : null }
- {errorMsg} +
{errors?.filter(e => e.field === input.split("#item_")[1].split("_json")[0])?.map(e => e.message)?.join(',')}
); }; diff --git a/app/models/field/complex_datation.rb b/app/models/field/complex_datation.rb index 162b7913e..fc7222545 100644 --- a/app/models/field/complex_datation.rb +++ b/app/models/field/complex_datation.rb @@ -170,7 +170,13 @@ def edit_props(item) componentPolicies: { modal: "super-editor" + }, + errors: item[:item].errors.map do |error| + { + message: error.message, + field: error.attribute } + end } end diff --git a/app/models/field/date_time.rb b/app/models/field/date_time.rb index aaa47b98e..497a86d94 100644 --- a/app/models/field/date_time.rb +++ b/app/models/field/date_time.rb @@ -159,6 +159,17 @@ def sql_type "JSON" end + def edit_props(item) + { + errors: item[:item].errors.map do |error| + { + message: error.message, + field: error.attribute + } + end + } + end + private def transform_value(v) diff --git a/app/views/catalog_admin/items/_common_form_fields.html.erb b/app/views/catalog_admin/items/_common_form_fields.html.erb index 07e35c800..0c39e7436 100644 --- a/app/views/catalog_admin/items/_common_form_fields.html.erb +++ b/app/views/catalog_admin/items/_common_form_fields.html.erb @@ -13,11 +13,6 @@ :current_user => @current_user, :catalog => @catalog ) %> - <% if field.editor_component.present? %> - <% @item.errors.where(field.uuid).each do |error| %> -
<%= error.message %>
- <% end %> - <% end %> <% end %>
From ad67efe8baf3cc2e0d72db9a3847b77c8c76adb6 Mon Sep 17 00:00:00 2001 From: Manuel Wegria Date: Mon, 6 May 2024 11:01:33 +0200 Subject: [PATCH 4/8] fix translations --- config/locales/app/en.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/locales/app/en.yml b/config/locales/app/en.yml index 7b40949fa..196afdf39 100644 --- a/config/locales/app/en.yml +++ b/config/locales/app/en.yml @@ -65,9 +65,9 @@ en: item: attributes: base: - cant_be_blank: "Doit etre rempli" - negative_dates: "Les dates négatives doivent être saisies avec l'option avant Jesus Christ (si activée)" - wrong_format: "Ne respecte pas le format %{field_format}" + cant_be_blank: "Must be present" + negative_dates: "Negative dates must be entered with the option before Jesus Christ (if activated)" + wrong_format: "Do not respect the format %{field_format}" field/choice_set: attributes: choice_set_id: From 5204f59985e7842edf6d97bf39393bdf08312e0a Mon Sep 17 00:00:00 2001 From: Manuel Wegria Date: Mon, 13 May 2024 11:54:02 +0200 Subject: [PATCH 5/8] update validation to reflect react component --- app/models/field/complex_datation.rb | 9 +++++---- app/models/field/date_time.rb | 8 ++++---- config/locales/app/en.yml | 2 +- config/locales/app/fr.yml | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/models/field/complex_datation.rb b/app/models/field/complex_datation.rb index fc7222545..28ddfd609 100644 --- a/app/models/field/complex_datation.rb +++ b/app/models/field/complex_datation.rb @@ -296,11 +296,12 @@ def validate(record) from_date_is_positive = value['from'].compact.except("BC").all? { |_, v| v.to_i >= 0 } to_date_is_positive = value['to'].compact.except("BC").all? { |_, v| v.to_i >= 0 } - invalid_format = field.format.chars.any? do |char| - value['to'][char].blank? || value['to'][char].nil? || value['from'][char].blank? || value['from'][char].nil? - end + allowed_formats = Field::ComplexDatation::FORMATS.select{|f| field.format.include?(f) || field.format == f} + + current_from_format = field.format.chars.map {|char| value['from'][char].blank? || value['from'][char].nil? ? nil : char}.compact.join + current_to_format = field.format.chars.map {|char| value['to'][char].blank? || value['to'][char].nil? ? nil : char}.compact.join - record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: field.format)) if invalid_format + record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: allowed_formats)) unless allowed_formats.include?(current_from_format) || allowed_formats.include?(current_to_format) record.errors.add(attrib, :negative_dates) if !to_date_is_positive || !from_date_is_positive end end diff --git a/app/models/field/date_time.rb b/app/models/field/date_time.rb index 497a86d94..072e50234 100644 --- a/app/models/field/date_time.rb +++ b/app/models/field/date_time.rb @@ -205,6 +205,7 @@ def validate(record) field = Field.find_by(uuid: attrib) return if value.blank? + return if value.is_a?(Hash) && value.has_key?("raw_value") return if value.keys.all? { |key| value[key].blank? || value[key].nil? } && !field.required if value.keys.all? { |key| value[key].blank? || value[key].nil? } && field.required @@ -212,11 +213,10 @@ def validate(record) return end - invalid_format = field.format.chars.any? do |char| - value[char].blank? || value[char].nil? - end + allowed_formats = Field::DateTime::FORMATS.select{|f| field.format.include?(f) || field.format == f} + current_format = field.format.chars.map {|char| value[char].blank? || value[char].nil? ? nil : char}.compact.join - record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: field.format)) if invalid_format + record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: allowed_formats)) unless allowed_formats.include?(current_format) end end end diff --git a/config/locales/app/en.yml b/config/locales/app/en.yml index 196afdf39..eb151ede2 100644 --- a/config/locales/app/en.yml +++ b/config/locales/app/en.yml @@ -67,7 +67,7 @@ en: base: cant_be_blank: "Must be present" negative_dates: "Negative dates must be entered with the option before Jesus Christ (if activated)" - wrong_format: "Do not respect the format %{field_format}" + wrong_format: "Do not respect the formats %{field_format}" field/choice_set: attributes: choice_set_id: diff --git a/config/locales/app/fr.yml b/config/locales/app/fr.yml index dd6836aac..d5d4bdc6b 100644 --- a/config/locales/app/fr.yml +++ b/config/locales/app/fr.yml @@ -69,7 +69,7 @@ fr: base: cant_be_blank: "Doit etre rempli" negative_dates: "Les dates négatives doivent être saisies avec l'option avant Jesus Christ (si activée)" - wrong_format: "Ne respecte pas le format %{field_format}" + wrong_format: "Ne respecte pas les formats %{field_format}" field/choice_set: attributes: choice_set_id: From 2ec04221eb637277b09833844a3e567ee79c2233 Mon Sep 17 00:00:00 2001 From: Manuel Wegria Date: Wed, 15 May 2024 09:31:02 +0200 Subject: [PATCH 6/8] fix translation --- config/locales/app/fr.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/app/fr.yml b/config/locales/app/fr.yml index 8a67ff99c..124c66d8e 100644 --- a/config/locales/app/fr.yml +++ b/config/locales/app/fr.yml @@ -67,7 +67,7 @@ fr: item: attributes: base: - cant_be_blank: "Doit etre rempli" + cant_be_blank: "Doit être rempli(e)" negative_dates: "Les dates négatives doivent être saisies avec l'option avant Jesus Christ (si activée)" wrong_format: "Ne respecte pas les formats %{field_format}" field/choice_set: From 3bcca6b4ba9db337e49a46e1c5ed253be8d774eb Mon Sep 17 00:00:00 2001 From: Manuel Wegria Date: Wed, 15 May 2024 11:12:56 +0200 Subject: [PATCH 7/8] fix back and front validation to validate both dates --- .../components/ComplexDatationInput.jsx | 30 ++++++++++--------- app/models/field/complex_datation.rb | 6 ++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx b/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx index 313ce36c1..6e76a1186 100644 --- a/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx +++ b/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx @@ -111,9 +111,12 @@ const ComplexDatationInput = (props) => { setToState(to); }, [granularity]) - let dateValid = isCurrentFormatValid(); - let errorStl = dateValid ? {} : {border: "2px solid #f00"}; - let errorMsg = dateValid ? "" : "Invalid value"; + let fromDateValid = isCurrentFormatValid('from'); + let toDateValid = isCurrentFormatValid('to'); + let errorStl = { + "from": fromDateValid ? {} : {border: "2px solid #f00"}, + "to": toDateValid ? {} : {border: "2px solid #f00"} + }; let fmt = getFieldOptions().format; function _handleChangeBC(input) { @@ -241,16 +244,16 @@ const ComplexDatationInput = (props) => { }); } - function getCurrentFormat() { + function getCurrentFormat(input) { let d = getData(); let f = getFieldOptions().format; return f.split('').map(function (k) { - return d['from'][k] ? k : d['to'][k] ? k : ''; + return d[input][k] ? k : ''; }).join('') } - function isCurrentFormatValid() { - let current = getCurrentFormat(); + function isCurrentFormatValid(input) { + let current = getCurrentFormat(input); if (current === '' && !isRequired) return true; // allow empty value if field is not required let allowed = getAllowedFormats(); return allowed.indexOf(current) > -1; @@ -275,13 +278,13 @@ const ComplexDatationInput = (props) => { ) : null} {fmt.includes('D') ? ( - ) : null } {fmt.includes('M') ? ( - ) : null } {fmt.includes('Y') ? ( - ) : null } {fmt.includes('h') ? ( - ) : null } {fmt.includes('m') ? ( - ) : null } {fmt.includes('s') ? ( - ) : null @@ -778,7 +781,6 @@ const ModalForm = (props) => {
{errorMsg}
-
diff --git a/app/models/field/complex_datation.rb b/app/models/field/complex_datation.rb index 28ddfd609..7b9013554 100644 --- a/app/models/field/complex_datation.rb +++ b/app/models/field/complex_datation.rb @@ -286,9 +286,9 @@ def validate(record) return if value.blank? return if value['selected_format'] != "date_time" - return if value['to'].keys.all? { |key| value['to'][key].blank? || value['to'][key].nil? } && value['from'].keys.all? { |key| value['from'][key].blank? || value['from'][key].nil? } && !field.required + return if (to_value_empty = value['to'].keys.all? { |key| value['to'][key].blank? || value['to'][key].nil? }) && (from_value_empty = value['from'].keys.all? { |key| value['from'][key].blank? || value['from'][key].nil? }) && !field.required - if value['to'].keys.all? { |key| value['to'][key].blank? || value['to'][key].nil? } && value['from'].keys.all? { |key| value['from'][key].blank? || value['from'][key].nil? } && field.required + if to_value_empty && from_value_empty && field.required record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.cant_be_blank')) return end @@ -301,7 +301,7 @@ def validate(record) current_from_format = field.format.chars.map {|char| value['from'][char].blank? || value['from'][char].nil? ? nil : char}.compact.join current_to_format = field.format.chars.map {|char| value['to'][char].blank? || value['to'][char].nil? ? nil : char}.compact.join - record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: allowed_formats)) unless allowed_formats.include?(current_from_format) || allowed_formats.include?(current_to_format) + record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: allowed_formats)) unless (allowed_formats.include?(current_from_format) || from_value_empty) && (allowed_formats.include?(current_to_format) || to_value_empty) record.errors.add(attrib, :negative_dates) if !to_date_is_positive || !from_date_is_positive end end From b9f2e98bdecf888f0061e23f7040ff4815a09ddf Mon Sep 17 00:00:00 2001 From: Manuel Wegria Date: Wed, 15 May 2024 16:47:34 +0200 Subject: [PATCH 8/8] fix back and front validations --- .../ComplexDatationInput/components/ComplexDatationInput.jsx | 1 + app/models/field/complex_datation.rb | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx b/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx index 6e76a1186..686ab0774 100644 --- a/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx +++ b/app/javascript/components/ComplexDatationInput/components/ComplexDatationInput.jsx @@ -255,6 +255,7 @@ const ComplexDatationInput = (props) => { function isCurrentFormatValid(input) { let current = getCurrentFormat(input); if (current === '' && !isRequired) return true; // allow empty value if field is not required + if (current === '' && isRequired && getCurrentFormat(input === "from" ? "to" : "from") !== '') return true; let allowed = getAllowedFormats(); return allowed.indexOf(current) > -1; } diff --git a/app/models/field/complex_datation.rb b/app/models/field/complex_datation.rb index 7b9013554..49ac12685 100644 --- a/app/models/field/complex_datation.rb +++ b/app/models/field/complex_datation.rb @@ -286,7 +286,9 @@ def validate(record) return if value.blank? return if value['selected_format'] != "date_time" - return if (to_value_empty = value['to'].keys.all? { |key| value['to'][key].blank? || value['to'][key].nil? }) && (from_value_empty = value['from'].keys.all? { |key| value['from'][key].blank? || value['from'][key].nil? }) && !field.required + to_value_empty = value['to'].keys.reject{|k| k=="BC"}.all? { |key| value['to'][key].blank? || value['to'][key].nil? } + from_value_empty = value['from'].keys.reject{|k| k=="BC"}.all? { |key| value['from'][key].blank? || value['from'][key].nil? } + return if to_value_empty && from_value_empty && !field.required if to_value_empty && from_value_empty && field.required record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.cant_be_blank'))