This project is archived and is in readonly mode.

#750 ✓stale
Matt Darby

Keeping track of validations with a hash

Reported by Matt Darby | August 3rd, 2008 @ 02:48 PM | in 3.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.

  • Maxim Chernyak

    Maxim Chernyak April 30th, 2009 @ 08:38 AM

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

  • Matt Darby

    Matt Darby April 30th, 2009 @ 11:51 AM

    Didn't think there was sufficient interest...

  • Erik Peterson

    Erik Peterson May 5th, 2009 @ 04:18 PM

    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.

  • Maxim Chernyak

    Maxim Chernyak May 5th, 2009 @ 04:40 PM

    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.

  • Matt Darby

    Matt Darby May 5th, 2009 @ 04:42 PM

    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.

  • Maxim Chernyak

    Maxim Chernyak May 5th, 2009 @ 04:43 PM

    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)
  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • rails

    rails April 5th, 2011 @ 01:00 AM

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

  • rails

    rails April 5th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”
  • af001

    af001 May 5th, 2011 @ 02:54 AM

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

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages