jeudi 7 mai 2015

Is it possible to have different custom validation error message fields for your nested form fields and the global error messages?

Running Rails 3.2.x with the nested_form gem, I have the usual error messages with ActiveRecord validations in the model such as:

class SomeModel
  validates :time, :presence => true, :format => { :with => /[0-9]?[0-9]:[0-9][0-9]/ }
end

It results in the following (the text in blue in the pictures is just descriptive text made with a photo editor to tell you waht's going on).

What I'd like to do

1) Display a custom message of my choosing such as "This model has blank fields" without the usual Rails predicate. If try using the :message options in the validates helper, I end up with an error message saying something like "SomeModel is This model has blank fields". Basically, it appends the custom message to the model name. How do I only display my custom message up top?

2) Display a custom error message for the nested form input field(s) that is different from the custom error message up top of the page. How do I display a different custom message such as "This is the error message for the nested input field" generated by the nested_form input field?

Errors at the top of the page

enter image description here

Error messages near the nested_form fields

enter image description here

Aucun commentaire:

Enregistrer un commentaire