Hi, I''m writing my own FormBuilder and I wanted to put an asterisk or something next to any required fields. The Builder has access to the AR object, is there any method I can call on the AR object that will tell me whether or not a particular field is required? Failing that, can I access the complete list of validations so I can look through it for instances of validates_presence_of? Jon
Julian ''Julik'' Tarkhanov
2006-Jun-05 18:12 UTC
[Rails] ActiveRecord validations and FormBuilder
On 5-jun-2006, at 14:25, Jon Evans wrote:> Hi, > > I''m writing my own FormBuilder and I wanted to put an asterisk or > something next to any required fields. The Builder has access to > the AR object, is there any method I can call on the AR object that > will tell me whether or not a particular field is required? > Failing that, can I access the complete list of validations so I > can look through it for instances of validates_presence_of?Validations cannot be introspected, primarily because they might be conditional/procedure dependent. A validation might _exist_ or might _not_ exist depending on th state of the record. -- Julian ''Julik'' Tarkhanov please send all personal mail to me at julik.nl
You might find the code in my plugin helpful ... Heck, why not just use the whole thing? :) http://railsrtv.rubyforge.org/ -- View this message in context: http://www.nabble.com/ActiveRecord-validations-and-FormBuilder-t1734939.html#a4721023 Sent from the RubyOnRails Users forum at Nabble.com.
Hi Julian, On 5 Jun 2006, at 19:11, Julian ''Julik'' Tarkhanov wrote:> On 5-jun-2006, at 14:25, Jon Evans wrote: > >> I''m writing my own FormBuilder and I wanted to put an asterisk or >> something next to any required fields. The Builder has access to >> the AR object, is there any method I can call on the AR object >> that will tell me whether or not a particular field is required? >> Failing that, can I access the complete list of validations so I >> can look through it for instances of validates_presence_of? > > Validations cannot be introspected, primarily because they might be > conditional/procedure dependent. A validation might _exist_ or > might _not_ exist depending on th state of the record.I guessed as much. I got round the problem by passing some extra parameters to the function: <% tabular_form_for :user, @user, :required => [:email, :login] do |f| %> [...] <%= f.text_field :email %> <%= f.text_field :login %> [...] <% end %> and each field_helper can see if a required field is being rendered by testing @options[:required].include?(field.to_sym). Jon