英文:
how to show alert errors during validation of unique field in modal form using laravel
问题
我正在使用Laravel,并且我是新手。我在"货币"视图中有一个模态表单,一切都正常,但我遇到了一个问题,当我点击保存按钮并且"CurrencyName"重复时,我会收到一个警告,但是我是在关闭模态表单并返回到"货币"页面后才收到这个警告的。
以下是在"货币"视图页面中的代码:
<div class="d-flex my-xl-auto right-content">
<a class="modal-effect btn-block" data-effect="effect-scale" data-toggle="modal" href="#modaldemo8">
<button type="button" class="btn btn-success btn-icon ml-2">
<i class="mdi mdi-plus"></i>
</button>
</a>
</div>
<div class="modal" id="modaldemo8" style="display: none;" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content modal-content-demo">
<div class="modal-header">
<h6 class="modal-title">新建</h6>
<button aria-label="Close" class="close" data-dismiss="modal" type="button">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="needs-validation was-validated" action="{{ route('currencies.store') }}" method="POST">
@csrf
<div class="row row-sm">
<div class="col-lg-12">
<div class="form-group row">
<label for="txtcurrencytype" class="col-lg-2 col-form-label">类型</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtcurrencytype" name="txtcurrencytype" value="forg" disabled>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyname" class="col-lg-2 col-form-label">货币名称</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtcurrencyname" name="CurrencyName" required>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyprice1" class="col-lg-2 col-form-label">价格1</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="txtcurrencyprice1" name="txtcurrencyprice1" step="any" required>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyprice2" class="col-lg-2 col-form-label">价格2</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="txtcurrencyprice2" name="txtcurrencyprice2" step="any" required>
</div>
</div>
<!-- 错误 -->
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- 错误 -->
</div>
</div>
<div class="modal-footer">
<button class="btn ripple btn-primary" type="submit">保存</button>
<button class="btn ripple btn-secondary" data-dismiss="modal" type="button">取消</button>
</div>
</form>
</div>
</div>
</div>
</div>
控制器部分:
public function store(Request $request)
{
$validatedData = $request->validate(
[
'CurrencyName' => ['unique:tblcurrencies', 'max:50']
],
[
'CurrencyName.unique' => '错误1!',
'CurrencyName.max' => '错误2!'
]
);
Currency::create(
[
'CurrencyRef' => Helper::GetNewRef(),
'CurrencyName' => $request['CurrencyName'],
'CurrencyPrice1' => $request['txtcurrencyprice1'],
'CurrencyPrice2' => $request['txtcurrencyprice2'],
'CurrentUser' => auth()->id()
]
);
if ($request->ajax()) {
return response()->json(['success' => true]);
}
return redirect()->back();
}
我需要的是,当我点击"保存"按钮时(如果有任何错误),我希望在模态表单中显示警告,而不关闭模态表单。
英文:
I'm working with Laravel and I'm new there.
I have a modal form in currencies view everything is ok but I have a problem that when I click on save button and the "CurrencyName" is duplicate I got an alert but I got that alert after closing the modal form and get back to "currencies" page.
the following code in "currencies" view page:
<div class="d-flex my-xl-auto right-content">
<a class="modal-effect btn-block" data-effect="effect-scale" data-toggle="modal" href="#modaldemo8">
<button type="button" class="btn btn-success btn-icon ml-2">
<i class="mdi mdi-plus"></i>
</button>
</a>
</div>
<div class="modal" id="modaldemo8" style="display: none;" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content modal-content-demo">
<div class="modal-header">
<h6 class="modal-title">new</h6><button aria-label="Close" class="close" data-dismiss="modal" type="button"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<form class="needs-validation was-validated" action="{{ route('currencies.store') }}" method="POST">
@csrf
<div class="row row-sm">
<div class="col-lg-12">
<div class="form-group row">
<label for="txtcurrencytype" class="col-lg-2 col-form-label">type</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtcurrencytype" name="txtcurrencytype" value="forg" disabled>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyname" class="col-lg-2 col-form-label">currency name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtcurrencyname" name="CurrencyName" required>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyprice1" class="col-lg-2 col-form-label">price1</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="txtcurrencyprice1" name="txtcurrencyprice1" step="any" required>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyprice2" class="col-lg-2 col-form-label">price2</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="txtcurrencyprice2" name="txtcurrencyprice2" step="any" required>
</div>
</div>
<!-- Errors -->
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- Errors -->
</div>
</div>
<div class="modal-footer">
<button class="btn ripple btn-primary" type="submit">save</button>
<button class="btn ripple btn-secondary" data-dismiss="modal" type="button">cancel</button>
</div>
</form>
</div>
</div>
</div>
</div>
and the controller:
public function store(Request $request)
{
$validatedData = $request->validate(
[
'CurrencyName' => ['unique:tblcurrencies', 'max:50']
],
[
'CurrencyName.unique' => 'err1!',
'CurrencyName.max' => 'err2!'
]
);
Currency::create(
[
'CurrencyRef' => Helper::GetNewRef(),
'CurrencyName' => $request['CurrencyName'],
'CurrencyPrice1' => $request['txtcurrencyprice1'],
'CurrencyPrice2' => $request['txtcurrencyprice2'],
'CurrentUser' => auth()->id()
]
);
return redirect()->back();
}
what I need is when I click on Save button (if there are any errors I want to display the alert in the modal form without closing the modal form)
答案1
得分: 0
实际上,这个问题在这里待了10天,没有任何答案!我是网页开发新手,但我尝试了很多解决方案,最终成功了。
$(document).ready(function() {
$('#xform').on('submit', function(e) {
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: formData,
success: function(response) {
location.reload();
},
error: function(xhr, status, error) {
var errors = xhr.responseJSON.errors;
if (errors) {
$('#errs').empty();
$.each(errors, function(key, value) {
$('#errs').append('<li>' + value + '</li>');
});
$('#errs').show();
if (!$('#modaldemo8').hasClass('show')) {
$('#modaldemo8').modal('show');
}
}
},
});
});
});
英文:
actually, that question was here for 10 days without any answer! I am new in web development but I tried many solutions and finally I got it.
$(document).ready(function() {
$('#xform').on('submit', function(e) {
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: formData,
success: function(response) {
location.reload();
},
error: function(xhr, status, error) {
var errors = xhr.responseJSON.errors;
if (errors) {
$('#errs').empty();
$.each(errors, function(key, value) {
$('#errs').append('<li>' + value + '</li>');
});
$('#errs').show();
if (!$('#modaldemo8').hasClass('show')) {
$('#modaldemo8').modal('show');
}
}
},
});
});
});
and you should delete the following part:
<!-- Errors -->
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- Errors -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论