我正在L5.2中创建一个CRUD系统,并使用Bootstrap Modal来显示表单。我使用 BootForms和 Laravel Bootstrap Modal Form来验证表单并在Modal内显示错误消息,而不关闭它。
基本上,我在同一页的Modal中打开添加/编辑表单,在Modal内完成表单验证后,将数据插入数据库,然后Modal关闭。之后,页面刷新并显示更新的数据。
除了成功时无法在我的页面上显示成功消息之外,一切都运作良好。
以下是我的代码:
AJAX
在成功的情况下,您可以看到AJAX只是重新加载页面,我希望它重定向到控制器函数中指定的页面并显示成功消息。
基本上,我在同一页的Modal中打开添加/编辑表单,在Modal内完成表单验证后,将数据插入数据库,然后Modal关闭。之后,页面刷新并显示更新的数据。
除了成功时无法在我的页面上显示成功消息之外,一切都运作良好。
以下是我的代码:
AJAX
$.ajax({
type: "POST",
url: url,
data: data,
dataType: 'json',
cache: false,
contentType: contentType,
processData: false
// Response.
}).always(function(response, status) {
// Reset errors.
resetModalFormErrors();
// Check for errors.
if (response.status == 422) {
var errors = $.parseJSON(response.responseText);
// Iterate through errors object.
$.each(errors, function(field, message) {
console.error(field+': '+message);
var formGroup = $('[name='+field+']', form).closest('.form-group');
formGroup.addClass('has-error').append('<p class="help-block">'+message+'</p>');
});
// Reset submit.
if (submit.is('button')) {
submit.html(submitOriginal);
} else if (submit.is('input')) {
submit.val(submitOriginal);
}
// If successful, reload.
} else {
location.reload();
}
});
控制器:
public function store(CustomerRequest $request)
{
Customer::create($request->all());
return redirect('customers')
->with('message', 'New customer added successfully.')
->with('message-type', 'success');
}
视图:(显示成功消息)
@if(Session::has('message'))
<div class="alert alert-{{ Session::get('message-type') }} alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
<i class="glyphicon glyphicon-{{ Session::get('message-type') == 'success' ? 'ok' : 'remove'}}"></i> {{ Session::get('message') }}
</div>
@endif
在成功的情况下,您可以看到AJAX只是重新加载页面,我希望它重定向到控制器函数中指定的页面并显示成功消息。
$request->session()->flash('message-type', 'success');
,但它没有生效。这是为什么? - shigg