Fix file size validation message

This commit is contained in:
Ismael MOUSSA S 2021-09-14 19:04:25 +02:00 committed by Pierre de La Morinerie
parent bdbb4deb87
commit 7c7c9c9ea4
9 changed files with 45 additions and 16 deletions

View file

@ -23,8 +23,11 @@ class AttestationTemplate < ApplicationRecord
validates :footer, length: { maximum: 190 } validates :footer, length: { maximum: 190 }
FILE_MAX_SIZE = 1.megabytes FILE_MAX_SIZE = 1.megabytes
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE } file_size_validation = Proc.new do
validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE } { less_than: FILE_MAX_SIZE, message: I18n.t('errors.messages.file_size_out_of_range', file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(FILE_MAX_SIZE)) }
end
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation.call
validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation.call
DOSSIER_STATE = Dossier.states.fetch(:accepte) DOSSIER_STATE = Dossier.states.fetch(:accepte)

View file

@ -28,19 +28,21 @@ class Avis < ApplicationRecord
has_one :procedure, through: :experts_procedure has_one :procedure, through: :experts_procedure
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
file_size_validation = Proc.new do
{ less_than: FILE_MAX_SIZE, message: I18n.t('errors.messages.file_size_out_of_range', file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(FILE_MAX_SIZE)) }
end
validates :piece_justificative_file, validates :piece_justificative_file,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: FILE_MAX_SIZE } size: file_size_validation.call
validates :introduction_file, validates :introduction_file,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: FILE_MAX_SIZE } size: file_size_validation.call
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
validates :claimant, presence: true validates :claimant, presence: true
validates :piece_justificative_file, size: { less_than: FILE_MAX_SIZE } validates :piece_justificative_file, size: file_size_validation.call
validates :introduction_file, size: { less_than: FILE_MAX_SIZE } validates :introduction_file, size: file_size_validation.call
before_validation -> { sanitize_email(:email) } before_validation -> { sanitize_email(:email) }
default_scope { joins(:dossier) } default_scope { joins(:dossier) }

View file

@ -21,7 +21,11 @@ class Champs::PieceJustificativeChamp < Champ
FILE_MAX_SIZE = 200.megabytes FILE_MAX_SIZE = 200.megabytes
validates :piece_justificative_file, validates :piece_justificative_file,
size: { less_than: FILE_MAX_SIZE }, size: {
less_than: FILE_MAX_SIZE,
message: I18n.t('errors.messages.file_size_out_of_range',
file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(FILE_MAX_SIZE))
},
if: -> { !type_de_champ.skip_pj_validation } if: -> { !type_de_champ.skip_pj_validation }
validates :piece_justificative_file, validates :piece_justificative_file,

View file

@ -30,7 +30,11 @@ class Champs::TitreIdentiteChamp < Champ
# #
validates :piece_justificative_file, validates :piece_justificative_file,
content_type: ACCEPTED_FORMATS, content_type: ACCEPTED_FORMATS,
size: { less_than: FILE_MAX_SIZE } size: {
less_than: FILE_MAX_SIZE,
message: I18n.t('errors.messages.file_size_out_of_range',
file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(FILE_MAX_SIZE))
}
def main_value_name def main_value_name
:piece_justificative_file :piece_justificative_file

View file

@ -27,7 +27,11 @@ class Commentaire < ApplicationRecord
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
validates :piece_jointe, validates :piece_jointe,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: FILE_MAX_SIZE } size: {
less_than: FILE_MAX_SIZE,
message: I18n.t('errors.messages.file_size_out_of_range',
file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(FILE_MAX_SIZE))
}
default_scope { order(created_at: :asc) } default_scope { order(created_at: :asc) }
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }

View file

@ -242,6 +242,9 @@ class Procedure < ApplicationRecord
validates_with MonAvisEmbedValidator validates_with MonAvisEmbedValidator
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
file_size_validation = Proc.new do
{ less_than: FILE_MAX_SIZE, message: I18n.t('errors.messages.file_size_out_of_range', file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(FILE_MAX_SIZE)) }
end
validates :notice, content_type: [ validates :notice, content_type: [
"application/msword", "application/msword",
"application/pdf", "application/pdf",
@ -254,7 +257,7 @@ class Procedure < ApplicationRecord
"image/jpg", "image/jpg",
"image/png", "image/png",
"text/plain" "text/plain"
], size: { less_than: FILE_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 2, 28) } ], size: file_size_validation.call, if: -> { new_record? || created_at > Date.new(2020, 2, 28) }
validates :deliberation, content_type: [ validates :deliberation, content_type: [
"application/msword", "application/msword",
@ -265,12 +268,15 @@ class Procedure < ApplicationRecord
"image/jpg", "image/jpg",
"image/png", "image/png",
"text/plain" "text/plain"
], size: { less_than: FILE_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 4, 29) } ], size: file_size_validation.call, if: -> { new_record? || created_at > Date.new(2020, 4, 29) }
LOGO_MAX_SIZE = 5.megabytes LOGO_MAX_SIZE = 5.megabytes
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'],
size: { less_than: LOGO_MAX_SIZE }, size: {
if: -> { new_record? || created_at > Date.new(2020, 11, 13) } less_than: LOGO_MAX_SIZE,
message: I18n.t('errors.messages.file_size_out_of_range',
file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(LOGO_MAX_SIZE))
}, if: -> { new_record? || created_at > Date.new(2020, 11, 13) }
validates :api_entreprise_token, jwt_token: true, allow_blank: true validates :api_entreprise_token, jwt_token: true, allow_blank: true
validates :api_particulier_token, format: { with: /\A[A-Za-z0-9\-_=.]{15,}\z/ }, allow_blank: true validates :api_particulier_token, format: { with: /\A[A-Za-z0-9\-_=.]{15,}\z/ }, allow_blank: true

View file

@ -100,6 +100,7 @@ ignore_unused:
- 'activerecord.attributes.*' - 'activerecord.attributes.*'
- 'activerecord.errors.*' - 'activerecord.errors.*'
- 'errors.messages.blank' - 'errors.messages.blank'
- 'errors.messages.content_type_invalid'
- 'pluralize.*' - 'pluralize.*'
- 'views.pagination.*' - 'views.pagination.*'
- 'time.formats.default' - 'time.formats.default'

View file

@ -0,0 +1,5 @@
en:
errors:
messages:
content_type_invalid: "is not of an accepted type"
file_size_out_of_range: "is too big. The file must be at most %{file_size_limit}."

View file

@ -1,5 +1,5 @@
fr: fr:
errors: errors:
messages: messages:
content_type_invalid: nest pas dun type accepté content_type_invalid: "nest pas dun type accepté"
file_size_out_of_range: "est trop lourde, elle doit faire au plus 200 Mo." file_size_out_of_range: "est trop lourd(e). Le fichier doit faire au plus %{file_size_limit}."