I have a Laravel User
model which has a unique validation rule on username
and email
. In my Repository, when I update the model, I revalidate the fields, so as to not have a problem with required rule validation:
public function update($id, $data) { $user = $this->findById($id); $user->fill($data); $this->validate($user->toArray()); $user->save(); return $user; }
This fails in testing with:
ValidationException: {"username":["The username has already been taken."],"email":["The email has already been taken."]}
Is there a way of fixing this elegantly?
So you can use simple unique validation in laravel. The following unique validation rule is simple. And you can use it as follow: The following unique validation rules with column name on controller methods, you can use as follow: Before using unique validation with the rule, you need to import use Illuminate\Validation\Rule;.
The filter validator, which uses PHP's filter_var function, ships with Laravel and was Laravel's default email validation behavior prior to Laravel version 5.8. The dns and spoof validators require the PHP intl extension. ends_with: foo, bar ,...
Each form request generated by Laravel has two methods: authorize and rules. As you might have guessed, the authorize method is responsible for determining if the currently authenticated user can perform the action represented by the request, while the rules method returns the validation rules that should apply to the request's data:
The field under validation must be numeric. The field under validation must match the authenticated user's password. This rule was renamed to current_password with the intention of removing it in Laravel 9. Please use the Current Password rule instead.
Append the id
of the instance currently being updated to the validator.
Pass the id
of your instance to ignore the unique validator.
In the validator, use a parameter to detect if you are updating or creating the resource.
If updating, force the unique rule to ignore a given id:
//rules 'email' => 'unique:users,email_address,' . $userId,
If creating, proceed as usual:
//rules 'email' => 'unique:users,email_address',
Another elegant way...
In your model, create a static function:
public static function rules ($id=0, $merge=[]) { return array_merge( [ 'username' => 'required|min:3|max:12|unique:users,username' . ($id ? ",$id" : ''), 'email' => 'required|email|unique:member'. ($id ? ",id,$id" : ''), 'firstname' => 'required|min:2', 'lastname' => 'required|min:2', ... ], $merge); }
Validation on create:
$validator = Validator::make($input, User::rules());
Validation on update:
$validator = Validator::make($input, User::rules($id));
Validation on update, with some additional rules:
$extend_rules = [ 'password' => 'required|min:6|same:password_again', 'password_again' => 'required' ]; $validator = Validator::make($input, User::rules($id, $extend_rules));
Nice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With