yaml_record
yaml_record copied to clipboard
Add validations
Add validation support to yaml_record. Simplest will be to be able to define valid?
and then have it run on update_attributes
and save
:
class Post < YamlRecord::Base
def valid?
errors add :title, "cannot be blank" if title.blank?
errors.add :description, "cannot be blank" if self.description.blank?
end
end
and then:
p = Post.new.save => false
p.errors # => { :title => ["cannot be blank"], :description => ["cannot be blank"] }
We can also add helpers as well:
class Post < YamlRecord::Base
validates_presence_of :title
validates_presence_of :description
end
which will automatically set:
p = Post.new.save => false
p.errors # => { :title => ["cannot be blank"], :description => ["cannot be blank"] }