我已经研究了这个问题的答案一段时间,但对于我的一生,我不知道为什么这不起作用。
我正在寻找的是一种使用模式对话框向控制器提交表单并进行验证的方法,而不需要刷新页面。然而,这是我第一次让 json 等工作。下面是我在此过程中找到的一些代码,并试图融入我的网站,但我发现缺少该主题
看法
<div class="modal fade" id="login" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
{{ Form::open(array('url' => 'accounts/login', 'method' => 'post', 'class' => 'form-horizontal', 'id' => 'login')) }}
<div class="modal-body">
@if ($message = Session::get('error'))
<div class="alert alert-error alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
{{ $message }}
</div>
@endif
{{-- Email --}}
<div class="control-group">
{{ Form::label('email', 'Email', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::email('email', Request::old('email'), array('placeholder' => 'Email')) }}
</div>
</div>
{{-- Password --}}
<div class="control-group">
{{ Form::label('password', 'Password', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::password('password', array('placeholder' => 'Password')) }}
</div>
</div>
</div>
{{-- Form Actions --}}
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{{ Form::submit('Login', array('class' => 'btn btn-primary')) }}
</div>
{{ Form::close() }}
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
JS
<script>
$(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('<div class="alert alert-error"><strong>'+ value +'</strong><div>');
}
});
$("#validation-errors").show();
} else {
console.log('worked');
}
},
error: function(xhr, textStatus, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
return false;
});
});
</script>
和控制器
public function postLogin()
{
$rules = array (
'email' => 'required|email',
'password' => 'required'
);
$validation = \Validator::make(\Input::all(), $rules);
if ( !$validation->fails() )
{
if( !\Request::ajax() )
{
return \Response::json(['success' => false, 'errors' => $validation->getMessageBag()->toArray()]);
} else {
try
{
$user = \Sentry::getUserProvider()->findByLogin(\Input::get('email'));
$throttle = \Sentry::getThrottleProvider()->findByUserId($user->id);
$throttle->check();
$credentials = ['email' => \Input::get('email'), 'password' => \Input::get('password')];
$user = \Sentry::authenticate($credentials);
} catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
\Session::flash('error', 'Invalid username or password.');
return \Response::json(['success' => false]);
} catch (\Cartalyst\Sentry\Users\UserNotActivatedException $e) {
\Session::flash('error', 'You have not yet activated this account.');
return \Response::json(['success' => false]);
}
catch (\Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
$time = $throttle->getSuspensionTime();
\Session::flash('error', "Your account has been suspended for $time minutes.");
return \Response::json(['success' => false]);
} catch (Cartalyst\Sentry\Throttling\UserBannedException $e) {
\Session::flash('error', 'You have been banned.');
return \Response::json(['success' => false]);
}
return \Response::json(['success' => true]);
return \Redirect::to('/');
}
} else {
\Session::flash('error', 'Enter some details.');
return \Response::json(['success' => false, 'errors' => $validation->getMessageBag()->toArray()], 400);
}
}
工作原理:模态按预期加载和显示。没有什么:没有验证响应,甚至没有提交的耳语。
谢谢
编辑:
我忘记添加路由并通过控制器中的一些错误刷新它,对控制器进行了以下更改:
public function postLogin()
{
$rules = array (
'email' => 'required|email',
'password' => 'required'
);
$validation = \Validator::make(\Input::all(), $rules);
if ( !$validation->fails() )
{
if( !\Request::ajax() )
{
return \Response::json(array('success' => false, 'errors' => $validation->getMessageBag()->toArray()));
} else {
try
{
$user = \Sentry::getUserProvider()->findByLogin(\Input::get('email'));
$throttle = \Sentry::getThrottleProvider()->findByUserId($user->id);
$throttle->check();
$credentials = array('email' => \Input::get('email'), 'password' => \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' => false));
} catch (\Cartalyst\Sentry\Users\UserNotActivatedException $e) {
\Session::flash('error', 'You have not yet activated this account.');
return \Response::json(array('success' => false));
}
catch (\Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
$time = $throttle->getSuspensionTime();
\Session::flash('error', "Your account has been suspended for $time minutes.");
return \Response::json(array('success' => false));
} catch (Cartalyst\Sentry\Throttling\UserBannedException $e) {
\Session::flash('error', 'You have been banned.');
return \Response::json(array('success' => false));
}
return \Response::json(array('success' => true));
return \Redirect::to('/');
}
} else {
\Session::flash('error', 'Enter some details.');
return \Response::json(array('success' => false, 'errors' => $validation->getMessageBag()->toArray()), 400);
}
}
它不喜欢,[]
而是我将其更改为array()
,页面加载正常但没有再次发生