This project is archived and is in readonly mode.
Keeping track of validations with a hash
-
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.
-
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.
-
Michael Gee
I frequently use validates_format_of and validates_numericality_of to implicitly validate an attribute's presence.
-
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:
-
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]} -
Matt Darby
Looks awesome. Having this functionality is huge in DRYing up forms!
-
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.
-
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 => {} }} -
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.
-
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.
-
Matt Darby
Didn't think there was sufficient interest...
-
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.
-
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.
-
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.
-
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)
-
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.
-
rails
- State changed from open to stale
