Monday, 20 April 2015

rails validations validate html

# assume we have a :data attribute (an hstore hash) in our model that values have html
# data :hstore default({}), not null
validate :invalid_html?
def invalid_html?
self.data.each do |key, value|
doc = Nokogiri::HTML(value) {|config| config.strict }
if doc.errors.any?
errors.add(:base, "#{key}: #{doc.errors.join(", ")}")
end
end
end
# The problem is that Nokogiri its NOT very :strict
#2.1.3 :001 > Nokogiri::HTML("<a/><a/><a/>") {|config| config.strict }.errors.any?
# => false
2.1.3 :002 >
view raw gistfile1.rb hosted with ❤ by GitHub

No comments:

Post a Comment