Get Rails Model Validation

For a model that has validations in the model_name.rb file, how can I access these checks manually? I would like to prepare my form validation system, which will work together with the Rails built-in tricks, and I want everything to be as dry as possible. My main problem is that I need to do a server-side check before any of the form values ​​gets into the database (I use a multi-stage form).

Basically I am wondering if there is a way like

User.validations.each do |v|
    puts v.constraint.to_s + " " + v.message
end

Is there anything similar?

Thanks in advance.

+3
source share
3 answers

, ( ).

, ( false save, ).

  • , , valid?/invalid?.
+1

ActiveRecord ?, , . ? , - , . - field1 field2.

unless x.valid?
  [:field1,:field2].each do |field|
    yes_there_was_an_error if x.errors[field]
  end
end
0

.

, . validates_presence_of :username, :if => proc {|u| u.signup_step >= 2 }, signup_step - .

, , , , . , , . , , , . .

0

Source: https://habr.com/ru/post/1710600/


All Articles