Search code examples
ruby-on-railsvalidationmodal-dialogprototypejs

Rails Modal Form Validation


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 %>

Solution

  • 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).