Lazy registration with sinatra and datamapper
I have a user model in my Sinatra application that requires the user to enter an email a password and a confirmation of the password. Something along the lines of this:
class User
include DataMapper::Resource
property :id, Serial
property :email, String, :required => true, :unique => true, :format => :email_address,
property :name, String
property :hashed_password, String
property :salt, String
property :created_at, DateTime
attr_accessor :password, :password_confirmation
validates_presence_of :password_confirmation
validates_presence_of :password
validates_length_of :password, :min => 6
validates_confirmation_of :password
end
This works fine and all except I want to be able to create a user and associate other models to that user without the user entering anything but a name. After a while the user will have created a group and other users that are also associated with that group. Then I need the user to be able to enter an email address and a password and put those in the created fields in the database. I guess the validation doesn't occur until I save the user object I have created but I need to save the users in the database without the email and the password of course If they want to be able to login th开发者_StackOverflow中文版ey will need to provide the information necesarry for that.
Do I create my own validation methods or is there a way to make DataMapper validate sometimes (eg. for one post request but not for another) and sometimes not?
Check out http://datamapper.org/docs/validations and "Contextual Validations" - maybe that's what you're looking for.
精彩评论