Possible to check if form field is required?

Hi list,
is it possible to check if form field will be required when form is
submitted (through validates_presence_of or some other validates_*
method)? I could do this manually but it doesn’t seems to me very DRY.

For example, I would like to be able to dynamically add asterisk with
some helper method if city is required here:

City*: <%= text_field ‘accomodation’, ‘city’ %>

thanks for answer,
Bojan M.


Bojan M.
Informatika Mihelac, Bojan M. s.p. | www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org

Bojan M. wrote:

thanks for answer,
Bojan M.

Solution would be to add code before each validation class methods which
would mark this field as required, for example:

module ActiveRecord

module Validations
module ClassMethods
alias :real_validates_presence_of :validates_presence_of unless
method_defined?(:real_validates_presence_of)
def validates_presence_of(*attr_names)
self.add_required_field(attr_names)
real_validates_presence_of(attr_names)
end
end
end
end

I tested that and it works, but I am not sure if that’s smartest way to
do it (I know that validations can be conditional). Anyone would like to
comment?

Bojan M.


Bojan M.
Informatika Mihelac, Bojan M. s.p. | www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs