Skip to content

Commit

Permalink
add validation for product's title
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyAndriyovuch committed Sep 17, 2023
1 parent 1a95a02 commit ac600df
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Product < ApplicationRecord
has_many :prices, as: :priceable, dependent: :destroy
has_many :categories_by_prices, through: :prices, source: :category

validates :title, presence: true, length: { in: 2..50 }, uniqueness: true
validates :title, presence: true, uniqueness: true, format: { with: /\A[a-zA-Zа-яієїґ'А-ЯІЄЇҐ0-9-\s]+\z/ }
validates :title, length: { in: 2..30 }, if: -> { title.present? }

scope :ordered_by_title, -> { order(:title) }

Expand Down
3 changes: 2 additions & 1 deletion config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ en:
blank: can't be blank
too_long: too long (maximum %{count} symbols)
too_short: too short (minimum %{count} symbols)
taken: "has already been taken, please choose another one"
taken: has already been taken, please choose another one
invalid: contains invalid characters
form_errors:
one: "One error prohibited %{model} from being saved"
other: "%{count} errors prohibited this %{model} from being saved"
Expand Down
3 changes: 2 additions & 1 deletion config/locales/uk/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ uk:
few: занадто коротка (мінімум %{count} знаки)
many: занадто коротка (мінімум %{count} знаків)
other: занадто коротка (мінімум %{count} знаку)
taken: "вже викоритовується, будь ласка оберіть іншу"
taken: вже викориcтовується, будь ласка оберіть іншу
invalid: містить неприпустимі символи
user:
attributes:
avatar:
Expand Down

0 comments on commit ac600df

Please sign in to comment.