Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4589764
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:08:04+00:00 2026-05-21T22:08:04+00:00

Apologies if this is a really common and/or ridiculous question; I swear I’ve read

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:08:05+00:00Added an answer on May 21, 2026 at 10:08 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies for the open endedness of this question, but I really don't know what
Apologies if this is a silly question, I haven't really done anything with xml
This is really a pardon my ignorance question so apologies if it doesn't meet
My sincere apologies if this question has been asked, but I really couldn't find
I am sure this is really simple so apologies for the naive question. I
I'm fairly new to Rspec so apologies if this is a really simple question:
Firstly apologies if this is a really simple question but Git is absolutely brand
Apologies if this is too ignorant a question or has been asked before. A
Apologies if this question is a bit obscure, I've been banging my head against
Apologies if this is a dumb question. In previous ASP.NET projects, I've been using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.