Created
April 20, 2015 22:55
rails validations validate html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 > |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment