Forum: Ruby on Rails ActiveRecord validations and FormBuilder

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jon Evans (Guest)
on 2006-06-05 16:27
(Received via mailing list)
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 (Guest)
on 2006-06-05 22:45
(Received via mailing list)
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
puzzelv (Guest)
on 2006-06-05 23:49
(Received via mailing list)
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...
Sent from the RubyOnRails Users forum at Nabble.com.
Jon Evans (Guest)
on 2006-06-06 01:08
(Received via mailing list)
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
This topic is locked and can not be replied to.