yaml_record icon indicating copy to clipboard operation
yaml_record copied to clipboard

Add validations

Open nesquena opened this issue 13 years ago • 0 comments

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"] }

nesquena avatar Feb 01 '12 08:02 nesquena