views:

1441

answers:

5

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

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)

Staelen
+1  A: 

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

Mike Buckbee
+1  A: 

Hi, 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.

grimen
A: 

Has anyone given LiveValidation a try?

Mike
@Mike the best bet to get an answer on Stack Overflow is to start a new thread. This site works under the assumption that each thread has a *single* question with a series of answers. If you use the **Ask A Question** link at the top of the screen you'll be much more likely to get a response. The FAQ: http://stackoverflow.com/faq might provide some additional insight.
ahsteele
A: 

I'm a rails rookie.

I tried live-validation. Good: It reflects ActiveRecord & generates jquery validation. Bad: I can't figure out how to add ajax or custom validators (doesn't validate dates).

Now I'm trying formtastic & validatious-on-rails.

validatious-on-rails Bad: Project doesn't seem active, depends on XMLHttpRequest.js instead of jquery. Good: Validates dates, works with formtastic.

formtastic Good: More expressive than form_for, active project, the textarea widget uses prototype but I can't figure out how prototype gets included. Bad: Default styling is ugly, error styling doesn't match rails' default, I haven't figured out how to add ajax & custom validators.

I'm using jqueryui for my modal dialogs.

If I make progress beyond the above, I'll post.

kellerapps
kellerapps
kellerapps