#750 √ incomplete
Matt Darby

Keeping track of validations with a hash

Reported by Matt Darby | August 3rd, 2008 @ 02:48 PM | in 2.x

As it stands, there is no easy way to enumerate which attributes in a model are required.

This patch creates a new attribute #required_attributes which simply holds an array of required attributes as defined via #validates_presence_of

This could be used for dynamically styling form fields based on whether or not the field is mandatory.

Here is an example:

# schema.rb
create_table "people", :force => true do |t|
  t.string   "first_name"
  t.string "last_name"
  t.integer "age"
end


# person.rb
class Person < ActiveRecord::Base
  validates_presence_of :first_name, :last_name

end


Person.required_attributes
# => [:first_name, :last_name]


# CSS Styles
.mandatory_field {
  background-color: red;
}

.form_field {
  background-color: grey;
}


# people_helper.rb
module PeopleHelper
  
  def css_class(field_name)
    Person.required_attributes.include?(fieldname) ? 'mandatory_field' : 'form_field'
  end
  
end


# new / edit.html.erb
<%= form_for(@person) do |f| %>

  <div class = "<%= css_class(:first_name) %>">
    <%= f.text_field :first_name %>
  </div>

<% end %>

Comments and changes to this ticket

  • Matt Darby
  • Pratik

    Pratik August 3rd, 2008 @ 03:09 PM

    • → State changed from “new” to “incomplete”
    • Missing tests
    • There can be multiple calls to validates_presence_of. So it should append to the required_attributes array.
    • Missing documentation

    Thanks.

  • Erik Peterson

    Erik Peterson August 3rd, 2008 @ 03:23 PM

    +1 for the idea, but I'm not sure that I like this implementation.

    I think we generally need some way of looking at what validations are on a model. However, I'm not sure there's an easy way to do this without completely changing the way validations are added right now.

    If the patch goes in somewhat like it is now, I think the required_attributes declaration shouldn't go in base. Methods in validations.rb add in attr_accessors, so it shouldn't be a big deal to add one in the module, outside of a method.

  • Michael Gee

    Michael Gee August 3rd, 2008 @ 03:23 PM

    I frequently use validates_format_of and validates_numericality_of to implicitly validate an attribute's presence.

  • Erik Peterson
  • Erik Peterson

    Erik Peterson August 3rd, 2008 @ 06:36 PM

    • → Tag changed from “activerecord enhancement patch” to “activerecord enhancement patch tested”

    Ok, here's a patch that keeps track of validations in a hash of arrays. It is tested and documented.

    Usage:

  • Erik Peterson

    Erik Peterson August 3rd, 2008 @ 06:38 PM

    Sorry about that, apparently I can't read the formatting instructions:

    class Person < ActiveRecord::Base
      validates_uniqueness_of :name
      validates_presence_of :name, :city
    end
    
    Person.validations
    => {:uniqueness => [:name], :presence => [:name, :city]}
    
    
  • Matt Darby

    Matt Darby August 3rd, 2008 @ 06:45 PM

    Looks awesome. Having this functionality is huge in DRYing up forms!

  • Paweł Kondzior
  • José Valim

    José Valim August 4th, 2008 @ 09:44 PM

    • → Title changed from “Patch: ActiveRecord#required_attributes” to “Keeping track of validations with a hash”

    Very nice! +1

    I changed the title to reflect the changes in discussion.

    Wouldn't be nice with validations also store the options sent?

    class Person < ActiveRecord::Base

    validates_presence_of :name, :city

    validates_length_of :name, :within => 3..20,

    end

    Person.validations

    => {:presence => { :name => {}, :city => {} }, :length => { :name => { :within => 3..20 } }}

    I think it would be even more readable if it is "attribute oriented":

    Person.validations

    => {:name => { :presence => {}, :length => { :within => 3..20 } }, :city => { :presence => {} }}

    Why? This would DRY javascript code generation from our models completely.

  • José Valim

    José Valim August 4th, 2008 @ 09:47 PM

    Lighthouse formatting 1 x 0 me. Again:

    
    class Person < ActiveRecord::Base
      validates_presence_of :name, :city
      validates_length_of :name, :within => 3..20,
    end
    
    Person.validations
    => {:presence => { :name => {}, :city => {} }, :length => { :name => { :within => 3..20 } }}
    
    

    I think it would be even more readable if it is "attribute oriented":

    
    Person.validations
    => {:name => { :presence => {}, :length => { :within => 3..20 } }, :city => { :presence => {} }} 
    
    
  • Erik Peterson

    Erik Peterson August 4th, 2008 @ 09:55 PM

    I had originally thought that keeping track of options would require a complete OO approach, with Validation objects and all sorts of nastiness that isn't really necessary.

    If it can be done in a hash that's still manageable, I'm OK with that. I'm not sure it can be, though.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

Repository is at http://github.com/rails/rails

Check out the development master (Edge Rails):

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..

Shared Ticket Bins