2019-03-23 13:07:04 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class HtmlValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
return if value.blank?
|
2019-03-26 16:33:26 +00:00
|
|
|
errors = html_errors(value)
|
|
|
|
unless errors.empty?
|
|
|
|
record.errors.add(attribute, I18n.t('html_validator.invalid_markup', error: errors.first.to_s))
|
|
|
|
end
|
2019-03-23 13:07:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-03-26 16:33:26 +00:00
|
|
|
def html_errors(str)
|
|
|
|
fragment = Nokogiri::HTML.fragment(str)
|
|
|
|
fragment.errors
|
2019-03-23 13:07:04 +00:00
|
|
|
end
|
|
|
|
end
|