Created
February 12, 2011 04:17
-
-
Save kenmazaika/823503 to your computer and use it in GitHub Desktop.
Add Client-Side validation to ActiveResource
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
class User << ActiveResource | |
class ClientValidatedFields | |
attr_accessor :phone | |
include ActiveModel::Validations | |
validates :phone, :presence => true, :length => { :maximum => 10, :minimum => 10 }, :numericality => true | |
def initialize(*args) | |
(args.first.first || {}).each_pair do |k, v| | |
send("#{k}=".to_sym, v) if self.respond_to?("#{k}=".to_sym) | |
end | |
end | |
end | |
attr_accessor :client_validated_fields | |
schema do | |
string :phone | |
end | |
delegate :phone=, :to => :client_validated_fields | |
delegate :phone, :to => :client_validated_fields | |
def require_more_info | |
return ! ( client_validated_fields.valid? && valid? ) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment