我在以更新模式形式顯示錯誤訊息時遇到問題。我正在使用 laravel 驗證請求和 AJAX 在模態中提交表單。我想查看每個輸入錯誤的欄位的錯誤訊息。但是,我收到此錯誤:
給定資料無效
我檢查了網路選項卡,我在那里看到了錯誤,但我不知道為什么這沒有顯示在我的欄位中。
這是我的腳本`
function updatePassword(e, t)
{
e.preventDefault();
const url = BASE_URL '/admin/organizations/operators/updatePassword/' $(updatePasswordForm).find("input[name='id']").val();
var form_data = $(t).serialize();
// loading('show');
axios.post(url, form_data)
.then(response => {
notify(response.data.message, 'success');
$(updatePasswordModal).modal('hide');
// roleTable.ajax.reload()
})
.catch(error => {
const response = error.response;
if (response) {
if (response.status === 422)
validationForm(updatePasswordForm, response.data.errors);
else if(response.status === 404)
notify('Not found', 'error');
else
notify(response.data.message, 'error');
}
})
.finally(() => {
// loading('hide');
});
}
`
這是我的刀片檔案`
<form id="updatePasswordForm" onsubmit="updatePassword(event, this)">
<input type="hidden" name="id" value="">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"> {{ __('Update Password') }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label class="col-sm-4 col-form-label required">{{ __('New Password') }}</label>
<div class="col-sm-8">
<div class="row">
<div class="col-sm-12">
<div class="form-group @error('user.password') error @enderror">
<input type="password" class="form-control" id="password" name="user[password]" placeholder="{{ __('Password') }}" required>
</div>
</div>
</div>
@error('user.password')
<p class="error-message">{{ $message }}</p>
@enderror
</div>
</div>
<div class="form-group row">
<label class="col-sm-4 col-form-label required">{{ __('Confirm Password') }}</label>
<div class="col-sm-8">
<div class="row">
<div class="col-sm-12">
<div class="form-group @error('user.password_confirmation') error @enderror">
<input type="password" class="form-control" id="confirmPassword" name="user[password_confirmation]" placeholder="{{ __('Confirm Password') }}">
</div>
</div>
</div>
@error('user.password_confirmation')
<p class="error-message">{{ $message }}</p>
@enderror
</div>
</div>
</div>
<div class="modal-footer justify-content-center">
<button type="button" class="btn btn-secondary mr-3" data-dismiss="modal">{{ __('Close') }}</button>
<button type="submit" class="btn btn-primary">{{ __('Save') }} </button>
</div>
</div>
</form>
`
這是我的控制器:
`
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Organization\Operator\UpdatePasswordRequest;
use App\Models\OrganizationOperator;
use Illuminate\Http\Request;
use App\Services\Response;
use Exception;
use Illuminate\Support\Facades\Log;
class OrganizationController extends Controller
{
public function updateOperatorPassword(OrganizationOperator $operator, UpdatePasswordRequest $request)
{
try {
$data = $request->validated();
$user = $data['user'];
// dd($user['password']);
$operator->update([
'password' => bcrypt($user['password']),
]);
return Response::success(__('Successfully updated'));
} catch (Exception $e) {
Log::error($e->getMessage());
return Response::error(__('Unable to update'), [], 500);
}
}
}
`
這是我的請求驗證類:
`
<?php
namespace App\Http\Requests\Admin\Organization\Operator;
use Illuminate\Foundation\Http\FormRequest;
class UpdatePasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'id' => ['required', 'integer', 'exists:organization_operators,id'],
'user.password' => ['required', 'string', 'min:8', 'confirmed'],
];
}
}
`
uj5u.com熱心網友回復:
首先,我認為這個問題是因為您從客戶端(即 ajax/axios)發送請求。當您提交到服務器端(沒有 ajax/axios)時驗證作業,然后回應驗證將顯示到刀片/html。
在這種情況下,您必須使用 class/id 手動將錯誤設定為 html(在 jquery 中使用 innerHTML 或 .html())。
例如來自 server/api 的回應:
{ "error" : {
"user.password" : ["invalid password"],
...
}
}
在客戶端,在這種情況下,您需要使用 p 標簽將該訊息放入 html 標簽。
<p id="error-password" ></p>
$('#error-password').html(error["user.password"])
// or
$('#error-password').text(error["user.password"])
uj5u.com熱心網友回復:
最后我解決了這個問題。
這是我的刀片檔案:
<form id="updatePasswordForm" onsubmit="updatePassword(event, this)" >
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"> {{ __('Update Password') }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input name="id" type="hidden" class="form-control" value="">
<div class="form-group">
<label for="">{{ __('Password') }}</label>
<input name="password" type="password" class="form-control" id="password" aria-describedby="emailHelp" placeholder="{{ __('Password') }}" required>
<small class="form-text error-message"></small>
</div>
<div class="form-group">
<label for="">{{ __('Confirm Password') }}</label>
<input name="password_confirmation" type="password" class="form-control" id="password_confirmation" aria-describedby="emailHelp" placeholder="{{ __('Confirm Password') }}" required>
<small class="form-text error-message"></small>
</div>
</div>
<div class="modal-footer justify-content-center">
<button type="button" class="btn btn-secondary mr-3" data-dismiss="modal">{{ __('Close') }}</button>
<button type="submit" class="btn btn-primary">{{ __('Save') }} </button>
</div>
</div>
</form>
這是我的阿賈克斯:
<script type="text/javascript">
function updatePassword(e, t)
{
e.preventDefault();
const url = BASE_URL '/admin/organizations/operators/update-password-api/' $(updatePasswordForm).find("input[name='id']").val();
var form_data = $(t).serialize();
// loading('show');
axios.post(url, form_data)
.then(response => {
notify(response.data.message, 'success');
$(updatePasswordModal).modal('hide');
})
.catch(error => {
const response = error.response;
console.log(response.data.errors);
if (response) {
if (response.status === 422)
validationForm(updatePasswordForm, response.data.errors);
else if(response.status === 404)
notify('Not found', 'error');
else
notify(response.data.message, 'error');
}
})
.finally(() => {
// loading('hide');
});
}
</script>
這是我的 PasswordRequest 驗證類:
<?php
namespace App\Http\Requests\Admin\Organization\Operator;
use Illuminate\Foundation\Http\FormRequest;
class UpdatePasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'id' => ['required', 'integer', 'exists:users,id'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
];
}
}
這是我的控制器:
public function updateOperatorPasswordApi(User $user, UpdatePasswordRequest $request)
{
try {
$data = $request->validated();
$user->update([
'password' => bcrypt($data['password']),
]);
return Response::success(__('Successfully created'));
} catch (Exception $e) {
Log::error($e->getMessage());
return Response::error(__('Unable to create'), [], 500);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/531363.html
上一篇:一種屬性型別取決于其他屬性值
