我在编辑用户信息时遇到问题。我希望在密码字段中未输入任何内容时,密码列保持不变。例如,目前,当我只更改电子邮件时,密码字段将发送为空,并且该空字段将保存为密码列中的散列值。有办法解决这个问题吗?情况1:如果可能的话,我不希望密码更改与其他信息分开,我希望所有这些都在一个表单标记中正确工作。情况2:不应通过采用当前密码来更改密码,因为管理员无权访问用户的密码。
代码如下:
刀片模板:
<form id="userFRM" action="{{ isset($user->id) ? route('user.update') : route('auth.register') }}" method="post">
@csrf
@if (session('error_message'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-check2 me-1"></i> {{ session('error_message') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
</button>
</div>
@endif
@if (isset($user->id))
<input id="user_id" name="user_id" type="hidden" value="{{ $user->id }}">
@endif
<div class="mb-2 row">
<div class="form-floating mb-2 col-4">
<input type="text" name="username" class="form-control" id="username"
value="{{ isset($user->id) ? $user->username : old('username') }}" placeholder="Username" />
<label for="username">{{ trans('admin.username') }}</label>
<x-input-error :messages="$errors->get('username')" class="mt-2" />
</div>
<div class="form-floating mb-2 col-4">
<input type="text" name="first_name" class="form-control" id="firs_tname"
value="{{ isset($user->id) ? $user->first_name : old('first_name') }}" placeholder="First Name" />
<label for="firs_tname">{{ trans('auth.first_name') }}</label>
</div>
<div class="form-floating mb-2 col-4">
<input type="text" name="last_name" class="form-control" id="last_name"
value="{{ isset($user->id) ? $user->last_name : old('last_name') }}" placeholder="Last Name" />
<label for="last_name">{{ trans('auth.last_name') }}</label>
</div>
</div>
<div class="mb-2 row">
<div class="form-floating mb-2 col-4">
<input type="email" name="email" class="form-control" id="email"
value="{{ isset($user->id) ? $user->email : old('email') }}" placeholder="email" />
<label for="email">{{ trans('auth.email') }}</label>
<x-input-error :messages="$errors->get('email')" class="mt-2" />
</div>
<div class="form-floating mb-2 col-4">
<input type="password" name="password" class="form-control" id="password" placeholder="password" />
<label for="password">{{ trans('auth.password') }}</label>
<x-input-error :messages="$errors->get('password')" class="mt-2" />
</div>
<div class="form-floating mb-2 col-4">
<input type="password" name="password_confirmation" class="form-control" id="password_confirmation"
placeholder="password_confirmation" />
<label for="password_confirmation">{{ trans('auth.confirm_password') }}</label>
</div>
</div>
<div class="mb-2 row">
<div class="form-floating mb-2 col-4">
<select name="role" class="form-select " id="role">
@foreach ($roles as $role)
@if (isset($user->id))
<option value="{{ $role->id }}" {{ $role->id == $user->role_id ? 'selected' : '' }}>
{{ $role->title }}</option>
@else
<option value="{{ $role->id }}">{{ $role->title }}</option>
@endif
@endforeach
</select>
<label for="role">{{ trans('auth.user_role') }}</label>
</div>
<div class="my-5 col-4">
@if (isset($user->id))
<input type="checkbox" name="is_admin" class="form-check-input" id="is_admin"
{{ $user->is_admin == true ? 'checked' : '' }} />
<label for="is_admin" class="form-check-label">{{ trans('auth.is_admin') }}</label>
<input type="checkbox" class="form-check-input ms-5" name="verify_email" id="verify_email"
{{ isset($user->email_verified_at) ? 'checked' : '' }} />
<label for="verify_email" class="form-check-label">{{ trans('auth.verify_email') }}</label>
@else
<input type="checkbox" name="is_admin" class="form-check-input" id="is_admin" />
<label for="is_admin" class="form-check-label">{{ trans('auth.is_admin') }}</label>
<input type="checkbox" class="form-check-input ms-5" name="verify_email" id="verify_email" />
<label for="verify_email" class="form-check-label">{{ trans('auth.verify_email') }}</label>
@endif
</div>
<div class="my-5">
@if (isset($user->id))
@if (\App\Helper\ClaimsHelper::hasAccess(\App\Helper\ClaimsHelper::EDIT))
<x-primary-button>
{{ trans('users.edit') }}
</x-primary-button>
@endif
@else
@if (\App\Helper\ClaimsHelper::hasAccess(\App\Helper\ClaimsHelper::ADD))
<x-primary-button>
{{ trans('auth.sign_up') }}
</x-primary-button>
@endif
@endif
</div>
</form>
UI表单
控制器
public function update(UserUpdatRequest $request)
{
$data = $request->all();
$updated = $this->userRepository->update($data['user_id'], [
'username' => $data['username'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'is_admin' => ($request->is_admin == 'on') ? 1 : 0,
'role_id' => $data['role'],
'email_verified_at' => isset($request->verify_email) ? Carbon::now() : '',
]);
if (!$updated) {
return ControllerHelper::updateError();
}
return \redirect()->route('users.list');
}
需求验证
public function rules(): array
{
return [
'user_id' => 'required|exists:'.User::class.',id',
'username' => 'required|string| max:255|unique:users,username,'.\request()->user_id,
'first_name' => 'required|string| min:3| max:255',
'last_name' => 'required|string| min:3| max:255',
'email' => 'required|string| email| max:255| unique:' . User::class . ',email,'.\request()->user_id,
'password' => 'confirmed|max:32',
'role' => 'required|integer|exists:' . Role::class . ',id',
];
}
1条答案
按热度按时间roejwanj1#
您需要有条件地构造更新有效负载。