Skip to content

Commit

Permalink
move errors from base to attrib and display them on their correct place
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Wegria committed May 1, 2024
1 parent 801f843 commit 11e90ce
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions app/models/field/complex_datation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
5 changes: 3 additions & 2 deletions app/models/field/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,16 @@ 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

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
record.errors.add(attrib, I18n.t('activerecord.errors.models.item.attributes.base.wrong_format', field_format: field.format)) if invalid_format
end
end
end

5 changes: 5 additions & 0 deletions app/views/catalog_admin/items/_common_form_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
:current_user => @current_user,
:catalog => @catalog
) %>
<% if field.editor_component.present? %>
<% @item.errors.where(field.uuid).each do |error| %>
<div class="base-errors"><%= error.message %></div>
<% end %>
<% end %>
<% end %>

<hr>
Expand Down
4 changes: 3 additions & 1 deletion config/locales/app/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions config/locales/app/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 11e90ce

Please sign in to comment.