title: Using custom validator to compare two form fields
body: <p>I'm working on learning custom validations in rails and am attempting to apply it for the purpose of comparing two form fields.  So far, I can easily pass one value to the custom my custom validator which I've stored in <code>app/validation/pin_validator.rb</code> and it works fine.  But I'd <em>really</em> like to set up something like the following that would accept two pieces of data from the form:</p>
  
  <pre><code>class PinValidator &lt; ActiveModel::EachValidator
    def validate_each (record, attribute, value)
      if value1 &gt; value2
        record.errors[attribute] &lt;&lt; (options[:message] || "validator working")
      end
    end
  end
  </code></pre>
  
  <p>And call it from the model with something like this:</p>
  
  <pre><code>validates :pin_number, :id_string, pin: true
  </code></pre>
  
  <p>Where <code>:id_string</code> and <code>:pin_number</code> are both form fields and would be compared as <code>value1</code> and <code>value2</code></p>
  
  <p>Is there a way to do this?</p>
  
  <p>Many thanks in advance!</p>
  
  <p><strong>Note:</strong> The value comparison above really could be solved on the front-end with a little javascript.  The reason I'm asking for a serverside solution is that I ultimately intend to compare both values with a set of entries in the database.  The example above is just to distil my question to something simple.</p>