Reputation: 26494
I am using Lightbox Gone Wild to display a modal dialog with a form inside. I am using a vanilla New view. This works like a champ up until a user doesn't input valid form data. Invalid data causes the controller to direct the user to the New view directly with the error message. Obviously, I would prefer the error be returned to the modal, though I understand the reason the user is being directed to a regular New view with errors.
One obvious but impractical option would be to write custom client-side validation. Another would be to generate the client-side validation logic based on the Models validations. To that end, I found two infant plugins which utilize validation_reflection. While validatious-on-rails is literally weeks old client_side_validation seems to be abandoned. Finally, using form_remote_tag also seems promising as it performs an AJAX postback and that seems like it wouldn't do the refresh on error.
In summary I am looking the most conventional way of validating user input to a form presented to a user in a modal dialog and on error returning them to that dialog with the errors.
Code to Open Modal
<% link_to 'New Project...', new_project_path, :class => 'lbOn' %>
New View
<% form_for(@project) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_field :description %>
</p>
<p>
<%= f.submit 'Create' %><br />
<a href="#" class="lbAction" rel="deactivate">Cancel</a>
</p>
<% end %>
Upvotes: 1
Views: 5852
Reputation: 101
I'm the guy behind validatious-on-rails. My shot at this is that you in many cases need both client-side and AJAX. The previous tries to solve the client-side validations in a DRY way there where a lot of Rails validation options that got lost, but I'm trying to solve this as with client-side validations that respects Rails options as much as possible - only got some issues with :if/:unless and a few others for quite obvious reasons, it's not possible in most IRL cases. For these cases, and cases where u have to query the database because (say check for uniquness of an user id) you need AJAX. In your case you only need to do an AJAX-request with the input you want to validate, and return a RJS/JS template (will be returned as JSON body) that update the form content/messages/etc with regular dom manipulation.
Upvotes: 3
Reputation: 1420
Example without client side validation: http://www.jetthoughts.com/blog/tech/2014/08/27/5-steps-to-add-remote-modals-to-your-rails-app.html
Upvotes: 0
Reputation: 6983
I'm thinking you could return a RJS template that would update a preset div in your Modal for errors (instead of redirecting to new).
Upvotes: 2
Reputation: 7841
The conventional way will be to do a client side validation with javascript, AND a server side validation as well. For your case, the server side validation should be done using AJAX definitely
For javascript validation, you can try:
http://docs.jquery.com/Plugins/Validation
For server-side validation, you can try:
http://guides.rubyonrails.org/activerecord_validations_callbacks.html (validation)
http://railscasts.com/episodes/43-ajax-with-rjs (ajax)
Upvotes: 0