Reputation: 7579
i use a separate class for validation so it looks like
class UserValidation {
protected static $id;
protected static $rules = [
'email' => 'required|email|unique:users,email,{{ self::$id }}',
'password' => 'required|alpha_dash|min:4',
];
public static function validate($input, $id)
{
self::$id = $id;
return Validator::make($input, self::$rules);
}
}
so imagine a user wants to update only his password ,so he updates it > submit but then he receive the error this email is already taken
,because laravel cant read {{ self::$id }}
,so how do i solve something like that.
Upvotes: 0
Views: 1417
Reputation: 1234
This will do your trick
class UserValidation {
protected static $rules = [
'email' => 'required|email|unique:users,email',
'password' => 'required|alpha_dash|min:4',
];
public static function validate($input, $id = false)
{
$rules = self::$rules;
if ($id) {
$rules['email'] .= ",$id";
}
return Validator::make($input, $rules);
}
}
If you are passing an id it will appended to the rules array if not then a simple rules array will be used. I hope this is what you meant.
Upvotes: 1