Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Keeping track of validations with a hash

#750

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 %>

Reported by Matt Darby · August 3rd, 2008 @ 02:48 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Matt Darby
  2. Pratik
    Pratik
    • 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.

    August 3rd, 2008 @ 03:09 PM

  3. Erik Peterson
    Erik Peterson

    +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.

    August 3rd, 2008 @ 03:23 PM

  4. Michael Gee
    Michael Gee

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

    August 3rd, 2008 @ 03:23 PM

  5. Erik Peterson
  6. Erik Peterson
    Erik Peterson
    • 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:

    August 3rd, 2008 @ 06:36 PM

  7. Erik Peterson
    Erik Peterson

    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]}
    
    

    August 3rd, 2008 @ 06:38 PM

  8. Matt Darby
    Matt Darby

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

    August 3rd, 2008 @ 06:45 PM

  9. Paweł Kondzior
  10. José Valim
    José Valim
    • 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.

    August 4th, 2008 @ 09:44 PM

  11. José Valim
    José Valim

    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 => {} }} 
    
    

    August 4th, 2008 @ 09:47 PM

  12. Erik Peterson
    Erik Peterson

    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.

    August 4th, 2008 @ 09:55 PM

  13. Maxim Chernyak
    Maxim Chernyak

    +1 What's the hold up here?

    This will make testing validations a breeze. Things like should_validate_inclusion_of only need to assert that it's declared in the validations hash.

    April 30th, 2009 @ 08:38 AM

  14. Matt Darby
    Matt Darby

    Didn't think there was sufficient interest...

    April 30th, 2009 @ 11:51 AM

  15. Erik Peterson
    Erik Peterson

    I hadn't been following up because there were a couple of projects out there to refactor Rails validations in a much more major way, and I didn't want to step on anybody's toes.

    However, I don't think that any of those projects have gotten anywhere. I'm not sure where we stand from a Rails 3 perspective, but taking another look at Validations makes too much sense.

    I've implemented a full object-based validation system for another framework (which hasn't been released quite yet). I'd be happy to port it for Rails 3, but I'm not sure how much it meshes, philosophy-wise.

    May 5th, 2009 @ 04:18 PM

  16. Maxim Chernyak
    Maxim Chernyak

    In my opinion

    Cons: * Increased code complexity

    Pros: * Easier introspection * Straightforward testing * Flexible inheritance (undoing validations in subclasses) * Form styling/logic (knowing required fields, etc)

    I've needed all of these pros, so I'd say go for it.

    May 5th, 2009 @ 04:40 PM

  17. Matt Darby
    Matt Darby

    I posted this ticket nearly a year ago and I'd still love to have this feature. I'd update my attempt, but it sounds like Erik is on the case.

    May 5th, 2009 @ 04:42 PM

  18. Maxim Chernyak
    Maxim Chernyak

    Stupid formatting. Reposting lists (after previewing markdown in textmate).

    Cons:

    • Increased code complexity

    Pros:

    • Easier introspection
    • Straightforward testing
    • Flexible inheritance (undoing validations in subclasses)
    • Form styling/logic (knowing required fields, etc)

    May 5th, 2009 @ 04:43 PM

  19. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  20. rails
    rails
    • State changed from incomplete to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    April 5th, 2011 @ 01:00 AM

  21. rails
    rails
    • State changed from open to stale

    April 5th, 2011 @ 01:00 AM

  22. af001
    af001

    私の中で、総合評価のとっても低いアバアバクロホリスタークロ銀座店。アバクロは大好きなんですけどね。一昨日の東京駅付近での打ち合わせの後、散歩がてら久々に行ってきました。そしたらビックリ!相変わらアバクロず、踊っているだけの店員さんとかもいましたが、

    May 5th, 2011 @ 02:54 AM