Commit bc8e7b6e authored by gwendal's avatar gwendal
Browse files

better error message for invalid mac address (close #170)

parent 89a48ed5
......@@ -3,8 +3,8 @@ class ServiceMac < ActiveRecord::Base
before_validation :reformat_addr
validates :service, presence: true
validates :addr, presence: true, uniqueness: true,
format: {with: /\A(?:[0-9A-F]{2}:){5}[0-9A-F]{2}\z/}
validates :addr, presence: true
validate :addr_is_allowed
validate :ensure_mac_allowed_in_service
......@@ -20,6 +20,17 @@ class ServiceMac < ActiveRecord::Base
self.addr = self.addr.upcase
end
def addr_is_allowed
if !addr.match(/\A(?:[0-9A-F]{2}:){5}[0-9A-F]{2}\z/)
errors.add :addr, :invalid_format, regex_format: "/\A(?:[0-9A-F]{2}:){5}[0-9A-F]{2}\z/"
return false
end
if !ServiceMac.where(addr: addr).empty?
errors.add :addr, :already_taken_by, service: ServiceMac.where(addr: addr).first.service.login
return false
end
end
def ensure_mac_allowed_in_service
return if self.service.nil?
if not self.service.authenticate_by_mac? then
......
......@@ -409,6 +409,9 @@ fr:
attributes:
service:
unexpected_type: 'Type inattendu: %{type}'
addr:
invalid_format: "Le format est invalide. Il doit valider l'expression suivante : %{regex_format}"
already_taken_by: "Cette addresse est déjà prise par %{service}"
service_sale_template:
attributes:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment