1

I am trying to get validation to return in my modal using rails 3.2,twitter/bootstrap and formtastic, but I am at a loss as to how to do this.

Below is a quick sample of what my modal and form look like. In the model I require firstname and lastname. If the user hits 'submit' without putting in a firstname or lastname, it immediately takes the user to my actual registration page and notifies them of what is missing.

But I would like it to return to this modal and let the user know what is missing. How do I accomplish this?

:In my header, this is how I bring up the modal:

=link_to 'Register', "#new_registration_modal", :data => { :toggle => "modal",:backdrop => "static" }

#app/views/subscribers/registration/_new_modal.html.haml

#new_registration_modal.modal.hide.fade
  .modal-header
    Some Text
  .modal-body
    Some Text
    =semantic_form_for(:subscriber, :as => resource_name, :url => registration_path(resource_name),:html => { :class => "form-inline" } )  do |f|
      =f.input :email
      =f.input :firstname
      =f.input :lastname
      =f.input :password
      =f.input :password_confirmation
      =f.submit
  .modal-footer
    Some Text
4

1 回答 1

4

我找到了一个适合我的解决方案(使用 simple_form),也许可以帮助你:

我想在另一个控制器的模态中添加一个新客户端。

模态控制器的视图:

<div id="client_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="client_modal_label" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="client_modal_label">Create new client</h3>
  </div>
  <div class="modal-body">
    <%= simple_form_for Client.new, html: {"data-type" => :json}, remote: true do |f| %>
    <div id="error_explanation" style="display:none;"></div>

    <div class="form-inputs">
      # the inputs
      # ... and "closing" the modal

client_controller.create:_

def create
 # save the data
 if @client.save
    respond_to do |format|
          format.html { redirect_to anagrafe_index_path, :notice => "Client create!"}
          format.json { render json: @client, status: :created, location: @client }
    end
 else
    respond_to do |format| 
        format.html { render :action => 'new'}
        format.json { render json: @client.errors.full_messages, status: :unprocessable_entity }
    end
 end
end

引发模态的 js.coffee :

$ ()->
  $("form.new_client").on "ajax:success", (event, data, status, xhr) ->
    $("form.new_client")[0].reset()
    $('#client_modal').modal('hide')
    $('#error_explanation').hide()

  $("form.new_client").on "ajax:error", (event, xhr, status, error) ->
    errors = jQuery.parseJSON(xhr.responseText)
    errorcount = errors.length
    $('#error_explanation').empty()
    if errorcount > 1
      $('#error_explanation').append('<div class="alert alert-error">The form has ' + errorcount + ' errors.</div>')
    else
      $('#error_explanation').append('<div class="alert alert-error">The form has 1 error.</div>')
    $('#error_explanation').append('<ul>')
    for e in errors
      $('#error_explanation').append('<li>' + e + '</li>')
    $('#error_explanation').append('</ul>')
    $('#error_explanation').show()
于 2013-06-21T13:03:45.863 回答