Hi - try to use this approach if you're using Laravel 5.4
use Illuminate\Validation\Rule;
$this->validate($request, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
Hello,
I'd like to start by saying that I am totally new buy in Laravel.
The problem I have is related to form validation.
In my app, I have created an action that returns a view that contains a form, allowing the users to update their own registration information.
Among the options, they can update is the email address. What I'd like to validate, is if the new inserted email address exists in the database, and aboard the user account update.
for the moment my app is like that:
UsersController.php@edit
...
public function edit( $id = 0 ) {
$user = User::where( 'id', '=', $id )->first();
return view( 'super-admin.users.edit' )->with( 'user', $user );
}
...
UsersController.php@update
...
public function update( Request $request, User $user ) {
$this->validate(
$request,
[
'name' => 'required',
'email' => "required|email|unique:users,email,{$user->id}",
'current_password' => 'required|checkcurrentpass',
'new_password' => 'required',
'confirm_new_password' => 'required|same:new_password',
]
);
}
...
super-admin.users.edit View
...
<div class="form-group {{ $errors->has( 'email' ) ? 'has-error' : '' }}">
<label
for="email"
>
Email Address
</label>
<input
type="email"
class="form-control"
id="email"
name="email"
placeholder="Email Address"
required
value="{{ old( 'email', $user->email ) }}"
/>
@if( $errors->has( 'email' ) )
<span class="help-block">
{{ $errors->first( 'email' ) }}
</span>
@endif
</div>
...
Then if I try to enter the email address used by another account I don't get any error message.
Am I doing anything wrong? Should I try another approach?
Hi - try to use this approach if you're using Laravel 5.4
use Illuminate\Validation\Rule;
$this->validate($request, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
Please or to participate in this conversation.