0
我在編輯表單中存在驗證規則問題。我的表單域是name
,email
,password
和password_confirmation
。我UpdateUserRequest
類返回此規則:Laravel 5驗證規則required_with:password_confirmation
return [
'name' => ['required', 'max:50', 'min:3'],
'email' => ['required', 'email', 'unique:users,email,' .$this->route('users')],
'password' => ['required_with:password_confirmation', 'confirmed']
];
和更新方法UsersController
是:
public function update(Requests\UpdateUserRequest $request, $id)
{
$user = $this->users->findOrFail($id);
$user->fill($request->only('name', 'email', 'password'))->save();
return redirect(route('backend.users.edit', $user->id))->with('status', 'User has been updated.');
}
這是一種形式:
{!! Form::model($user, [
\t \t 'method' => $user->exists ? 'put' : 'post',
\t \t 'route' => $user->exists ? ['backend.users.update', $user->id] : ['backend.users.store']
\t ]) !!}
\t <div class="form-group">
\t \t {!! Form::label('name') !!}
\t \t {!! Form::text('name', null, ['class' => 'form-control']) !!}
\t </div>
\t <div class="form-group">
\t \t {!! Form::label('email') !!}
\t \t {!! Form::email('email', null, ['class' => 'form-control']) !!}
\t </div>
\t <div class="form-group">
\t \t {!! Form::label('password') !!}
\t \t {!! Form::password('password', ['class' => 'form-control']) !!}
\t </div>
\t <div class="form-group">
\t \t {!! Form::label('password_confirmation') !!}
\t \t {!! Form::password('password_confirmation', ['class' => 'form-control']) !!}
\t </div>
{!! Form::submit($user->exists ? 'Save User' : 'Create New User', ['class' => 'btn btn-primary btn-sm']) !!}
密碼驗證規則required_with:password_confirmation
應允許用戶如果不想更改它,則允許用戶保留空字段password
和password_confirmation
,但這不起作用。當我將該字段留空時,空值將被散列並存儲在數據庫中。所有其他驗證都可以正常工作,但當用戶在password
和password_confirmation
之間留下空值時不能正常工作。爲什麼這不起作用?我究竟做錯了什麼?