Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created February 12, 2011 04:17
Show Gist options
  • Save kenmazaika/823503 to your computer and use it in GitHub Desktop.
Save kenmazaika/823503 to your computer and use it in GitHub Desktop.
Add Client-Side validation to ActiveResource
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