Forums » General Forum »
Automatic validation might not always be desired
Added by Anonymous 194 days ago
Hi again,
I'm using a state machine to do the data input of my model step by step.
Therefore I don't want all models to be validated automatically but rather define validations for each of the steps.
I found it annoying that classy-inheritance tries to validate them automatically and had to unpack the gem once more to remove the appropriate code.
Maybe you should let your users decide on the validation behavior themselves.
Cheers
Pascal
Replies
RE: Automatic validation might not always be desired - Added by Andrew Stone 194 days ago
Pascal,
I would say that is an edge case for classy inheritance. Please remember this code was meant to simplify the standard associations common to many Rails applications. If you would like to enhance this functionality, please feel free to submit a patch. As you have seen, I am quick to turn around a new release.
I greatly appreciate your interest and assistance with getting classy inheritance to this stage.
thanks,
andy
btw, I have created a classy-inheritance-test-app on github that I should have completed in the next day or two.
RE: Automatic validation might not always be desired - Added by Geoff Garside 183 days ago
I'm looking at trying to add support for this kind of thing, just wondering though would :required => false or :optional => true make more sense?
Basically the method I'm looking at is just targetting the :if option on the validates_presence_of or possibly not calling it at all. This way it could handle methods, procs and strings as the :if option does. Not doing anything to mess with the validates_associated as I think with a state machine it could get quite messy. Instead I'm assuming that you'd fill in all of a depended model at once and validate it as a whole.
Does that make sense or just sound horrible in every single way?
RE: Automatic validation might not always be desired - Added by Geoff Garside 183 days ago
Actually gone with :required as it made more sense than :optional. I was only entertaining :optional as I initially believed it might be more elegant to code up but found a nice way of implementing :required and still nicely maintaining the current expected operation. I need to do some test but I'll send a pull request through in a bit.
RE: Automatic validation might not always be desired - Added by Andrew Stone 182 days ago
Thanks for your contribution, it is very much appreciated... especially the testing code.
I think I'm going to change the option from :required to :validates_if. I think that is clearer in regards to the functionality.
RE: Automatic validation might not always be desired - Added by Geoff Garside 182 days ago
Yes, :validates_if is definitely clearer, though you then need to make sure then that people understand it only disables the presence check and not the validation of the associated model
RE: Automatic validation might not always be desired - Added by Andrew Stone 182 days ago
I would think the validates_associated should be included in this as well, don't you? As it is right now, validates_associated is always called. If someone has :validates_if => false, validates_associated should be skipped as well.
Agree?
RE: Automatic validation might not always be desired - Added by Geoff Garside 182 days ago
Well validates_associated doesn't validate if the association is nil which is why the validates_presence_of is required. So I was needing more the case of a dependent model not initially being there at creation but if it is there I want it to be valid. Actually I think that's why I went with :required initially, as removing the validates_presence_of makes the association optional. I'd almost imagine them to be separate situations.
Perhaps :required and :allow_invalid are needed to allow the disabling of each case.
RE: Automatic validation might not always be desired - Added by Andrew Stone 182 days ago
Let's just simplify this then...
:validates_presence_of_if and :validates_associated_if
Both default to true.
How does that sound?
RE: Automatic validation might not always be desired - Added by Andrew Stone 182 days ago
I'm trying to avoid introducing completely new concepts and I think this will help.
RE: Automatic validation might not always be desired - Added by Geoff Garside 182 days ago
Yeah sounds good though perhaps drop the "of" so its :validates_presence_if
RE: Automatic validation might not always be desired - Added by Geoff Garside 182 days ago
Hoorah for Commit: 0b5ecf