validation and duplicate update() in laravel - laravel

In my form Voiture, I want to do a validation system for the fields immatriculation and num_vehicule
Here i an overview:
If I edit the first recording ie the value of the field num_vehicule 000001 per 0000032and that I validate, I have an error message because the value of the field immatriculation already exists.
I don't understand the problem... Do you have an idea please?
'immatriculation' => 'required|string|max:15|min:6|unique:voitures,immatriculation',
'num_vehicule' => 'required|string|max:6|min:6|unique:voitures,num_vehicule',
'fk_modele' => 'required'
Thank you

Use
'immatriculation' => 'string|max:255|unique:voitures,immatriculation,' . $this->voitures->id,
The syntax is
'input_field' => 'unique:<table name>,<column name for this input field>, <unique id>, <unique id column in table>';

Related

Laravel Validation of Array

Laravel Help with validation
Hi everyone I want to validate an array of a foreign key to make sure it exists I am doing
'activities' => 'required|array',
'activities.*' => 'exists:admin_activities,id',
but it's skipping this validation
any solution for this
You can validate each element of an array in Laravel with "dot notation" to For example, to validate that each id in a given array input field is exists in admin_activities , you may do the following:
'activities.*.id' => 'required|exists:admin_activities,id',

Don't validate if there is no edit - Laravel 7 [duplicate]

This question already has an answer here:
laravel unique name says already taken if you edit/update that particular object
(1 answer)
Closed 1 year ago.
I have an edit profile form where the inputs have values assigned by the user in the past. The problem I'm facing is that when the user does not change these values the validation mechanism of laravel throughs an error, e.g if username:Gass is not changed then the user clicks on submit and it's faced with the error: The username has already been taken.
How can I skip the validation if the input value has not been edited?
My validation
$request->validate([
'username' => ['nullable','max:15','unique:users', 'regex:/^[a-zA-Z0-9_-]+$/'],
'email' => ['nullable','string', 'email', 'max:50', 'unique:users'],
'about' => ['nullable','max:300'],
'location' => ['nullable','max:30','regex:/^[a-zA-Z0-9., ]+$/'],
'website' => ['nullable','max:40','regex:/^((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)$/'],
'avatar' => ['nullable','image','mimes:jpg,png,jpeg,gif','max:500','dimensions:min_width=100,min_height=100'],
'twitter' => ['nullable','max:15', 'regex:/^[a-zA-Z0-9_]+$/'],
'youtube' => ['nullable','max:50', 'regex:/^[a-zA-Z0-9_-]+$/'],
]);
You can force a Unique rule to ignore a given ID. See the Laravel documentation. Scroll to section: Forcing A Unique Rule To Ignore A Given ID.
In your example, something like this should suffice:
$request->validate([
...,
'username' => ['nullable','max:15',Rule::unique('users')->ignore(auth()->id()), 'regex:/^[a-zA-Z0-9_-]+$/'],
]);
the ignore() method accepts 2 parameters, the first being the value to check for, the second parameter accepts the column of the table to check, but defaults to id.
Assuming your user is logged in here.
in update, you must ignore that item for unique validation rule like
'username' => ['nullable','max:15',Rule::unique('users')->ignore($this->id, 'id'), 'regex:/^[a-zA-Z0-9_-]+$/'],

laravel validation check field equal something

how to check a field is exactly equal to a string or number
I want to check a field named course_id is equal a field of database id in course database. now I want to check if course_id is equal to id.
You can validate it by using the exists validation rule:
$validationRules = ['course_id' => 'exists:course,id'];
create rule as below and use validator on input
$rules = array(
'id' => 'exists:your_table_name'
);
for more help
https://laravel.com/docs/5.2/validation#rule-exists

Change default column name 'id' when validating

I am trying to validate an email field so that I make sure it's unique, but not in a row with a certain id, which I do like so:
'email' => 'required|email|unique:seller_user,email,'.$seller_id,
That works, but it automatically searches for a column named id, whereas in my table that column is actually called seller_id, so how can I change that?
You can specify the field to search using the fourth parameter to the rule:
'email' => 'required|email|unique:seller_user,email,'.$seller_id.',seller_id',

Laravel - email uniqueness validation fails

I have the following rules:
$rules = array(
'name' => 'required|alpha_num|unique:users,username',
'password' => 'required|min:6|confirmed',
'email' => 'required|email|unique:users'
);
unique checks for existing emails properly, HOWEVER, if I input an invalid email address (something like kolÄ™#ddd.com) I get a "Woops, something went wrong" laravel error. It seems that the validation doesn't stop at the email rule and the wrong email address is being passed to the unique rule. If I remove the unique rule it works properly, but doesn't check if a certain email exists. How do I solve this?
I've found out what was causing the validator to fail: mysql table row collation.
I've set it to utf8-general-ci and it's all working now.

Resources