Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix date time and complex datation validations #20

Draft
wants to merge 10 commits into
base: development
Choose a base branch
from
16 changes: 12 additions & 4 deletions app/models/field/complex_datation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(attrib, I18n.t('activerecord.errors.models.item.attributes.base.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(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
27 changes: 27 additions & 0 deletions app/models/field/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,31 @@ 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(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(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 %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je penses qu'il serait judicieux de déplacer cela dans les 2 components concernées (date et datation) car finalement ces spécifiques a ces 2 components ET l'erreur doit etre dans le form-group et non pas après.

D'ailleurs ca devrait afficher une double erreur sur un field "classique" non ? As-tu testé ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

la condition if field.editor_component.present? vérifie si c'est un composant react donc pas de doublons sinon c'est potentiellement possible de passer les erreurs dans les composants react mais plus compliqué

Copy link

@skyporter skyporter May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je comprends mais il est préférable de passer l'erreur au component car le dom ne va pas la. Au moins essaier.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j'ai passé l'erreur au composant react

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah top !

<% 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
2 changes: 2 additions & 0 deletions config/locales/app/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ fr:
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}"
field/choice_set:
attributes:
choice_set_id:
Expand Down