<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: validates code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 14:18:03 GMT</pubDate>
    <description>DZone Snippets: validates code</description>
    <item>
      <title>Custom validation with rails: words with more than 26 characters</title>
      <link>http://snippets.dzone.com/posts/show/5266</link>
      <description>This goes in whatever model you're validating.  "Subject" can be any of the symbols in your model and doesn't need the : in front of it.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def validate&lt;br /&gt;	if subject.split.any?{|w| w.length &gt; 26}&lt;br /&gt;		errors.add(:subject, "cannot have words more than 26 consecutive characters")&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 21 Mar 2008 19:40:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5266</guid>
      <author>ioda006 ()</author>
    </item>
    <item>
      <title>Validations for Non-ActiveRecord Objects</title>
      <link>http://snippets.dzone.com/posts/show/1055</link>
      <description>I needed to find a way to use active record validations on non active record items so i went searching and found this blog which is useful for me..&lt;br /&gt;Totally ripped off Peter Donald's blog :P&lt;br /&gt;http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects&lt;br /&gt;&lt;br /&gt;To get this working grab the active_form.rb file and place it in the app/models directory. You can then make your model objects extend ActiveForm and use them like regular ActiveRecord objects.&lt;br /&gt;&lt;br /&gt;active_form.rb&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Note ".valid?" method  must occur on object for validates_associated&lt;br /&gt;class ActiveForm&lt;br /&gt;  &lt;br /&gt;  def initialize(attributes = nil)&lt;br /&gt;    if attributes&lt;br /&gt;      attributes.each do |key,value|&lt;br /&gt;        send(key.to_s + '=', value)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    yield self if block_given?&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def [](key)&lt;br /&gt;    instance_variable_get("@#{key}")&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def method_missing( method_id, *args )&lt;br /&gt;    if md = /_before_type_cast$/.match(method_id.to_s)&lt;br /&gt;      attr_name = md.pre_match&lt;br /&gt;      return self[attr_name] if self.respond_to?(attr_name)&lt;br /&gt;    end&lt;br /&gt;    super&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;protected &lt;br /&gt;  def raise_not_implemented_error(*params)&lt;br /&gt;    ValidatingModel.raise_not_implemented_error(*params)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.human_attribute_name(attribute_key_name)&lt;br /&gt;    attribute_key_name.humanize&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def new_record?&lt;br /&gt;    true&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  # these methods must be defined before include&lt;br /&gt;  alias save raise_not_implemented_error&lt;br /&gt;  alias update_attribute raise_not_implemented_error&lt;br /&gt;&lt;br /&gt;public&lt;br /&gt;  include ActiveRecord::Validations&lt;br /&gt;&lt;br /&gt;protected &lt;br /&gt;&lt;br /&gt;  # the following methods must be defined after include so that they overide&lt;br /&gt;  # methods previously included&lt;br /&gt;  alias save! raise_not_implemented_error&lt;br /&gt;&lt;br /&gt;  class &lt;&lt; self&lt;br /&gt;    def raise_not_implemented_error(*params)&lt;br /&gt;      raise NotImplementedError&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    alias validates_uniqueness_of raise_not_implemented_error&lt;br /&gt;    alias create! raise_not_implemented_error&lt;br /&gt;    alias validate_on_create raise_not_implemented_error&lt;br /&gt;    alias validate_on_update raise_not_implemented_error&lt;br /&gt;    alias save_with_validation raise_not_implemented_error    &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;require 'dispatcher'&lt;br /&gt;class Dispatcher&lt;br /&gt;  class &lt;&lt; self&lt;br /&gt;    if ! method_defined?(:form_original_reset_application!) &lt;br /&gt;      alias :form_original_reset_application! :reset_application!&lt;br /&gt;      def reset_application!&lt;br /&gt;        form_original_reset_application!&lt;br /&gt;        Dependencies.remove_subclasses_for(ActiveForm) if defined?(ActiveForm)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jan 2006 02:01:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1055</guid>
      <author>Namor ()</author>
    </item>
    <item>
      <title>Defining a custom validates in rails</title>
      <link>http://snippets.dzone.com/posts/show/822</link>
      <description>in the model:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  require 'validations'&lt;br /&gt;&lt;br /&gt;  validates_positive_or_zero :number&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;in /lib/validations.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def validates_positive_or_zero(*attr_names)&lt;br /&gt;  configuration = { :message =&gt; "Cannot be negative" }&lt;br /&gt;  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;  validates_each attr_names do |m, a, v| m.errors.add(a, configuration[:message]) if v&lt;0 end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 19 Oct 2005 21:47:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/822</guid>
      <author>zzzrByte (Tal Ater)</author>
    </item>
  </channel>
</rss>
