Rails Formtastic does not show error message for date field
I have a Formtastic form like:
<%= semantic_form_for @event do |form| %>
<div class="field">
<%= form.label :starts_at %><br />
<%= form.datetime_select :starts_at, :discard_year => true, :minute_step => 30 %>
</div>
<% end %>
In my Event.rb, I have:
validates_inclusion_of :starts_at, :in => Time.now..(Time.now + 1.year),
:message => "appointment must be between now and one year from now"
It seems that the validation works (prevent instances with invalid date from being saved), but the error message does not show in the form. I have other fields with validations and they all show message that I specified in Event.rb
I wonder what I did wrong, or开发者_开发百科 there's something special about date field.
To see the error message you need to access the the errors method. I call a helper method inside the form:
render_form_errors(form)
Where the helper method is:
def render_form_errors(form)
object = form.object
if object && object.errors.any?
message = 'Please fix these errors, then try again:'.html_safe
content_tag :div, message + form.semantic_errors(*object.errors.keys), :class => "form-error ui-state-error ui-corner-all"
else
''
end
精彩评论