Hi all! Though ActiveRecord will have strong validation support in 0.9 as far as I know, I would like to know how to validate data that does not belong to an ActiveRecord class. Take for example a Login form or an email feedback form. I do not want to store those things in the database and thus I cannot make use of an ActiveRecord based validation. Any hints to point me into the right direction? :-) Yours, Lars -- "Stil ist die Fähigkeit, komplizierte Dinge einfach zu sagen - nicht umgekehrt." -- Cocteau, Jean
Hi all! Though ActiveRecord will have strong validation support in 0.9 as far as I know, I would like to know how to validate data that does not belong to an ActiveRecord class. Take for example a Login form or an email feedback form. I do not want to store those things in the database and thus I cannot make use of an ActiveRecord based validation. Any hints to point me into the right direction? :-) Yours, Lars -- "Stil ist die Fähigkeit, komplizierte Dinge einfach zu sagen - nicht umgekehrt." -- Cocteau, Jean
On Dec 16, 2004, at 3:18 PM, Lars Hoss wrote:> Hi all! > > Though ActiveRecord will have strong validation support in 0.9 as far > as I know, > I would like to know how to validate data that does not belong to an > ActiveRecord class. Take for example a Login form or > an email feedback form. I do not want to store those things in the > database and thus I cannot make use of an ActiveRecord based > validation. > > Any hints to point me into the right direction? :-)I have seen the same problem, not sure what the simple solution is. I''m used to a ''Command'' pattern for web apps; the object backing your form doesn''t have to be special in any regard. It is simply a holder for values. Validation and parameter binding/conversion are seperate issues that should not be combined in a one-does-all solution like AR now tries to be. It is called Seperation of Concerns :-) Validation is something generic and doesn''t have to do anything with web controllers or a persistence mechanism like ActiveRecord. It is probably a good idea to rip it out, put it in a ActiveValidation project and then let the other components use that :-) There are plenty of good reasons for this, like the problem Lars ran into. But also think of using validation outside of the web domain; what if i have an XML-RPC service as part of my application that accepts an Order record as one of its parameters. Wouldn''t it be great if you could use the same validation mechanism there? S.
On Thu, Dec 16, 2004 at 03:17:29PM +0100, Lars Hoss wrote:> Hi all! > > Though ActiveRecord will have strong validation support in 0.9 as far > as I know, > I would like to know how to validate data that does not belong to an > ActiveRecord class. Take for example a Login form or > an email feedback form. I do not want to store those things in the > database and thus I cannot make use of an ActiveRecord based > validation. > > Any hints to point me into the right direction? :-)You can create "virtual attributes", the trick being to call attr_accessor. Something like so: class Sprocket < ActiveRecord::Base attr_accessor :nizzle_not_in_db validates_format_of :nizzle_not_in_db, :with => /my nizzle/ # ... end What might serve your pursposes though is the new validates_confirmation_of and/or validates_acceptance_of macro style class methods that are going into AR in the 0.9 release which, [looks at his watch], should be happening right about *now*. Docs on these new methods are, for now, only in the source: http://dev.rubyonrails.org/trac.cgi/file/trunk/activerecord/lib/active_record/validations.rb marcel -- Marcel Molina Jr. <marcel-WRrfy3IlpWYdnm+yROfE0A@public.gmane.org>
> I have seen the same problem, not sure what the simple solution is. > I''m used to a ''Command'' pattern for web apps; the object backing your > form doesn''t have to be special in any regard. It is simply a holder > for values. Validation and parameter binding/conversion are seperate > issues that should not be combined in a one-does-all solution like AR > now tries to be. > > It is called Seperation of Concerns :-) > > Validation is something generic and doesn''t have to do anything with > web controllers or a persistence mechanism like ActiveRecord. It is > probably a good idea to rip it out, put it in a ActiveValidation > project and then let the other components use that :-)Agreed. Validation is not only useful for AR objects. What about plain-text files, XML files, LDAP and so on? As Stefan suggests it would be useful to extract the validation framework out of AR. This way it would be possible to make use of it without AR (although AR could make use of it be default of course).> There are plenty of good reasons for this, like the problem Lars ran > into. But also think of using validation outside of the web domain; > what if i have an XML-RPC service as part of my application that > accepts an Order record as one of its parameters. Wouldn''t it be great > if you could use the same validation mechanism there?I think so, yes. A validation framework should not be bound to the database layer only imho. There are many areas in which a validation framework would be useful. Perhaps someone should code a proof-of-concept? :-)) Yours, Lars -- "Stil ist die Fähigkeit, komplizierte Dinge einfach zu sagen - nicht umgekehrt." -- Cocteau, Jean