5

Apologies if this is a really common and/or ridiculous question; I swear I've read over the documentation multiple times and everything seems so focused on ActiveRecord to the point they've wandered off the path of forms that do something other than create or edit model data.

Take for example a form with inputs to control the extraction and display of some statistics. What does rails provide me with for validating the user input of this form, which won't be invoking save on any records? Things like:

  • :email must be an email address
  • :num_products must be a positive whole number
  • :gender must be one of "M" or "F"
  • :temperature must be between -10 and 120

Etc etc (the sort of stuff that comes standard in most web frameworks)...

Is there something in Rails for me to perform this arbitrary validation and some view helper to display a list of errors, or is everything coupled with ActiveRecord?

Apologies if I've overlooked this in the documentation, but this and this don't really cover it, at least as far as mt weary eyes can tell.

scratches head

Thanks to Rob's answer, here's what I've come up with. I created a utility class (aptly named Validator) which is just embedded into my controllers for anything that needs it.

module MyApp

    class Validator
        include ActiveModel::Validations
        include ActiveModel::Conversion
        extend  ActiveModel::Naming

        def initialize(attributes = {})
            super
            attributes.each { |n, v| send("#{n}=", v) if respond_to?("#{n}=") }
        end
    end

end

Now in the controller, for example, just define a little inline-class:

class LoginController < ApplicationController
    class AuthenticationValidator < MyApp::Validator
        attr_accessor :email
        attr_accessor :password
        validates_presence_of :email, :message => "E-mail is a required field"
        validates_presence_of :password, :message => "Password cannot be blank"
    end

    def authenticate
        if request.post?
            @validator = AuthenticationValidator.new(params)
            if @validator.valid?
                # Do something meaningful
            end
        end
    end

It feels a bit unnecessary to stick every single set of validation rules in their own .rb when the logic is more controller-oriented IMHO. There is probably a more succinct way to write this, but I'm pretty new to Ruby and Rails.

JW8
  • 1,496
  • 5
  • 21
  • 36
d11wtq
  • 34,788
  • 19
  • 120
  • 195

2 Answers2

6

Yes, it can be done fairly easily.

You can use the validations API for it.

As an example, here is a contact us model that I use for an application that is not using ActiveRecord.

class ContactUs
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :subject, :message
  validates_presence_of :name, :email, :message, :subject
  validates_format_of :email, :with => /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\z/
  def initialize(attributes=nil)
    attributes.each do |name, value|
      send("#{name}=", value)
    end unless attributes.nil?
  end

  def persisted?
    false
  end
end
Rob Di Marco
  • 43,054
  • 9
  • 66
  • 56
  • Excellent, thank you! I'll pick this apart :) I assume all I need to read up on is `ActiveModel::Validations` in the API reference? :) EDIT | Sorry, overlooked the embedded link. – d11wtq May 02 '11 at 14:21
  • This was exactly what I needed, thanks. I've used this approach to create a more generic "Validator" abstract class to remove the boilerplate code from the includes/initialize method. – d11wtq May 02 '11 at 15:05
0

There is a valid method on model, which triggers validation.

so instead model.save, try model.valid?

sparrovv
  • 6,894
  • 2
  • 29
  • 34
  • I'm specifically trying to understand if I can do validation on user input, not on a model. Let's say I'm not loading a model at all, and I'm actually interacting with something like the Twitter API... how would I validate the user input before making the API calls? I know I could do it manually, but it seems like something that surely must be there? :) – d11wtq May 02 '11 at 14:16
  • Another scenario is something like user login, where the fields are "email" and "password", and you want to fail form validation if the "email" param doesn't look like an email address. At this point you don't have a model to validate; all you have is some user input. – d11wtq May 02 '11 at 14:18
  • Oh, sorry, I didn't understand the question. I see that Rob has already answered the question correctly, so check out his answer. – sparrovv May 02 '11 at 14:22