Reputation: 1144
I use Simple_form in my Rails 4 application.
How can I display error messages in a view that is not tied to a model ?
I want to have the same result than in other views based on models.
For now, this is the code in the view :
<%= simple_form_for(:registration, html: { role: 'form' }, :url => registrations_path) do |f| %>
<%= f.error_notification %>
<%= f.input :name, :required => true, :autofocus => true %>
<%= f.input :email, :required => true %>
<%= f.input :password, :required => true %>
<%= f.input :password_confirmation, :required => true %>
<%= f.button :submit %>
<% end %>
In a 'normal' view (i.e. with a model) the line <%= f.error_notification %>
display errors.
What should I do in my controller to initialize something used by Simple_form to display errors ?
Thanks
Upvotes: 7
Views: 12135
Reputation: 163
Simple Form does not support this functionality "out of the box". But you can add it with a "monkey patch" in an initializer like this (disclaimer - this appears to work for my simple test case but has not been thoroughly tested):
// Put this code in an initializer, perhaps at the top of initializers/simple_form.rb
module SimpleForm
module Components
module Errors
def has_errors?
has_custom_error? || (object && object.respond_to?(:errors) && errors.present?)
end
def errors
@errors ||= has_custom_error? ? [options[:error]] : (errors_on_attribute + errors_on_association).compact
end
end
end
end
module SimpleForm
class ErrorNotification
def has_errors?
@options[:errors] || (object && object.respond_to?(:errors) && errors.present?)
end
end
end
And then you can add errors to your form like this (note you indicate whether to show the error notification by setting 'errors: true', you would have to perform your own check to decide if there are errors present, and add the errors dynamically):
=simple_form_for :your_symbol do |f|
=f.error_notification errors: true
=f.input :item1, as: :string, error: "This is an error on item1"
=f.input :item2, as: :string, error: "This is an error on item2"
Upvotes: 5
Reputation: 847
Use client_side_validations gem , it is simple , and you've to do only -
<%= simple_form_for(:registration, html: { role: 'form' }, :url => registrations_path) , :validate => true do |f| %>
But you need to add validations in model also.
Upvotes: -1
Reputation: 18037
The simple_form_for
helper must wrap a model. But just because we say this doesn't mean it has to be an ActiveRecord model that's backed by a database table. You're free to create models that aren't backed by a database. In Rails 3+ the way to do this is to have your class include the components that you need from ActiveModel
. This SO post explains how to do this with an example (and I'm sure there's many others out there). Once you have a model that includes ActiveModel::Validation
you can add to the errors
collection and then the f.error_notification
statement will output the errors as you're used to in table-backed models.
TL;DR: Create a non-ActiveRecord, non-table-backed model then treat it like a regular old model and the form should do the right thing.
Upvotes: 0