ActiveRecord validations and FormBuilder


#1

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


#2

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


#3

You might find the code in my plugin helpful … Heck, why not just use
the
whole thing? :slight_smile:
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.


#4

Hi Julian,

On 5 Jun 2006, at 19:11, Julian ‘Julik’ Tarkhanov wrote:

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