title: Laravel 4 bootstrap modal Sentry 2 login
body: <p>I have researched the answer to this question for some time but for the life of me I have no idea why this does not work.</p>
  
  <p>What I am looking for is a way to use the modal dialog to submit forms to the controller with validation, and not have a page refresh. However this is my first time getting json and the like to work. what I have below is a some code that I have found along the way and have tried to fit into my site, but I have found that the topic is lacking</p>
  
  <p>View</p>
  
  <pre><code>&lt;div class="modal fade" id="login" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"     aria-hidden="true"&gt;
        &lt;div class="modal-dialog"&gt;
            &lt;div class="modal-content"&gt;
                &lt;div class="modal-header"&gt;
                    &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;&amp;times;&lt;/button&gt;
                    &lt;h4 class="modal-title"&gt;Modal title&lt;/h4&gt;
                &lt;/div&gt;
                {{ Form::open(array('url' =&gt; 'accounts/login', 'method' =&gt; 'post', 'class' =&gt; 'form-horizontal', 'id' =&gt; 'login')) }}
                &lt;div class="modal-body"&gt;
                    @if ($message = Session::get('error'))
                    &lt;div class="alert alert-error alert-block"&gt;
                        &lt;button type="button" class="close" data-dismiss="alert"&gt;&amp;times;&lt;/button&gt;
                        {{ $message }}
                    &lt;/div&gt;
                    @endif
                    {{-- Email --}}
                    &lt;div class="control-group"&gt;
                        {{ Form::label('email', 'Email', array('class' =&gt; 'control-label')) }}
                        &lt;div class="controls"&gt;
                            {{ Form::email('email', Request::old('email'), array('placeholder' =&gt; 'Email')) }}
                        &lt;/div&gt;
                    &lt;/div&gt;
                    {{-- Password --}}
                    &lt;div class="control-group"&gt;
                        {{ Form::label('password', 'Password', array('class' =&gt; 'control-label')) }}
                        &lt;div class="controls"&gt;
                            {{ Form::password('password', array('placeholder' =&gt; 'Password')) }}
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                {{-- Form Actions --}}
                &lt;div class="modal-footer"&gt;
                    &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                    {{ Form::submit('Login', array('class' =&gt; 'btn btn-primary')) }}
                &lt;/div&gt;
                {{ Form::close() }}
  
            &lt;/div&gt;&lt;!-- /.modal-content --&gt;
        &lt;/div&gt;&lt;!-- /.modal-dialog --&gt;
    &lt;/div&gt;&lt;!-- /.modal --&gt;
  </code></pre>
  
  <p>The JS</p>
  
  <pre><code>&lt;script&gt;
        $(document).ready(function() {
            $('form#login').submit(function() {
                $.ajax({
                    url: 'http://onepro.com.au/accounts/login',
                    type: 'post',
                    cache: false,
                    dataType: 'json',
                    data: $('form#login').serialize(),
                    beforeSend: function() { console.log('Send before the request'); $("#validation-errors").hide().empty(); },
                    success: function(data) {
                        if(data.success == false)
                        {
                            var arr = data.error;
                            $.each(arr, function(index, value)
                            {
                                if (value.length != 0)
                                {
                                    $("#validation-errors").append('&lt;div class="alert alert-error"&gt;&lt;strong&gt;'+ value +'&lt;/strong&gt;&lt;div&gt;');
                                }
                            });
                            $("#validation-errors").show();
                        } else {
                            console.log('worked');
                        }
                    },
                    error: function(xhr, textStatus, thrownError) {
                        console.log(xhr.status);
                        console.log(thrownError);
                    }
                });
                return false;
            });
        });
    &lt;/script&gt;
  </code></pre>
  
  <p>And The controller</p>
  
  <pre><code>public function postLogin()
      {
          $rules = array (
              'email' =&gt; 'required|email',
              'password' =&gt; 'required'
          );
  
          $validation = \Validator::make(\Input::all(), $rules);
          if ( !$validation-&gt;fails() )
          {
              if( !\Request::ajax() )
              {
                  return \Response::json(['success' =&gt; false, 'errors' =&gt; $validation-&gt;getMessageBag()-&gt;toArray()]);
              } else {
                  try
                  {
                      $user = \Sentry::getUserProvider()-&gt;findByLogin(\Input::get('email'));
                      $throttle = \Sentry::getThrottleProvider()-&gt;findByUserId($user-&gt;id);
                      $throttle-&gt;check();
  
                      $credentials = ['email' =&gt; \Input::get('email'), 'password' =&gt; \Input::get('password')];
  
                      $user = \Sentry::authenticate($credentials);
                  } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
                      \Session::flash('error', 'Invalid username or password.');
                      return \Response::json(['success' =&gt; false]);
                  } catch (\Cartalyst\Sentry\Users\UserNotActivatedException $e) {
                      \Session::flash('error', 'You have not yet activated this account.');
                      return \Response::json(['success' =&gt; false]);
                  }
                  catch (\Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
                      $time = $throttle-&gt;getSuspensionTime();
                      \Session::flash('error', "Your account has been suspended for $time minutes.");
                      return \Response::json(['success' =&gt; false]);
                  } catch (Cartalyst\Sentry\Throttling\UserBannedException $e) {
                      \Session::flash('error', 'You have been banned.');
                      return \Response::json(['success' =&gt; false]);
                  }
                  return \Response::json(['success' =&gt; true]);
                  return \Redirect::to('/');
              }
          } else {
              \Session::flash('error', 'Enter some details.');
              return \Response::json(['success' =&gt; false, 'errors' =&gt; $validation-&gt;getMessageBag()-&gt;toArray()], 400);
          }
      }
  </code></pre>
  
  <p>What works: The modal loads and displays as expected.
  What doesn't: There is no response of validation or even a whisper of submit.</p>
  
  <p>Thanks</p>
  
  <p>EDIT:</p>
  
  <p>I had forgotten to add the route in and on refresh it through some errors in the controller the following change has been made to the controller:</p>
  
  <pre><code>public function postLogin()
      {
          $rules = array (
              'email' =&gt; 'required|email',
              'password' =&gt; 'required'
          );
  
          $validation = \Validator::make(\Input::all(), $rules);
          if ( !$validation-&gt;fails() )
          {
              if( !\Request::ajax() )
              {
                  return \Response::json(array('success' =&gt; false, 'errors' =&gt; $validation-&gt;getMessageBag()-&gt;toArray()));
              } else {
                  try
                  {
                      $user = \Sentry::getUserProvider()-&gt;findByLogin(\Input::get('email'));
                      $throttle = \Sentry::getThrottleProvider()-&gt;findByUserId($user-&gt;id);
                      $throttle-&gt;check();
  
                      $credentials = array('email' =&gt; \Input::get('email'), 'password' =&gt; \Input::get('password'));
  
                      $user = \Sentry::authenticate($credentials);
                  } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
                      \Session::flash('error', 'Invalid username or password.');
                      return \Response::json(array('success' =&gt; false));
                  } catch (\Cartalyst\Sentry\Users\UserNotActivatedException $e) {
                      \Session::flash('error', 'You have not yet activated this account.');
                      return \Response::json(array('success' =&gt; false));
                  }
                  catch (\Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
                      $time = $throttle-&gt;getSuspensionTime();
                      \Session::flash('error', "Your account has been suspended for $time minutes.");
                      return \Response::json(array('success' =&gt; false));
                  } catch (Cartalyst\Sentry\Throttling\UserBannedException $e) {
                      \Session::flash('error', 'You have been banned.');
                      return \Response::json(array('success' =&gt; false));
                  }
                  return \Response::json(array('success' =&gt; true));
                  return \Redirect::to('/');
              }
          } else {
              \Session::flash('error', 'Enter some details.');
              return \Response::json(array('success' =&gt; false, 'errors' =&gt; $validation-&gt;getMessageBag()-&gt;toArray()), 400);
          }
      }
  </code></pre>
  
  <p>It doesn't like <code>[]</code> instead I changed it to <code>array()</code>, the page loads fine but nothing happens again</p>