Laravel - email uniqueness validation fails - validation

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.

Related

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 compound validation rule

I would like to create a validation rule which checks if the combination of two values is unique.
There is a field for the street (id) and the house number. Both fields are required. Further, no new entry should be created if a certain combination of street and house number already exists.
How can I achieve this with Laravel?
What I have so far is only this:
protected $rules = [
'street_id' => 'required',
'tree_number' => 'required',
];
I guess this would be possible by using Rule Objects. Then I would query the DB if a certain combination is already stored. But can this be done in a simpler way as well?
Something like below
'street_id' => ['required', 'unique:table,street_id,'.$request->input('street_id').',NULL,id,tree_number,'.$request->input('tree_number')]

Problem with adding record to database after seeds in Laravel

I add test records to the database using seeds
public function run()
{
DB::table('categories')->insert([
['id' => 1,'name' => 'Select a category', 'slug' => null],
['id' => 2,'name' => 'Computers', 'slug' => 'computer-&-office'],
]);
}
But then, if I want to add a new record to the database, already through the form, I get the error
SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "categories_pkey"
I understand that when I add a new record through the form, it is created with id = 1, and I already have this id in the database. How can I avoid this error?
You should remove id from insert() and make it auto increment in mysql,
It complains about a unique constraint, meaning your primary key is indexed as "categories_pkey" or you have another field that is unique.
This happens because you are inserting a record and a record already exists where that column must be unique.
In general production workflow, when you add a record you never specify an ID. Most cases (there are exceptions) ID is an autoincrement integer, meaning it adds up automatically. On the first insert the database set its ID to 1, the second to 2 and so on.
As a seeder, its generally a good idea to set up the ID so you know that a certain ID matches a certain item (as a base content of a project like user states or roles).
As a regular workflow (from a form submission), you can have something like this
DB::table('categories')->insert([
['name' => 'some value', 'slug' => 'some slug']
]);
However, I don't advise to use DB::table when Laravel provides ActiveRecords pattern (ORM, called Eloquent) which you should take a look here.
https://laravel.com/docs/8.x/eloquent#introduction
Besides the benefits of layer abstraction and working with activerecords, It produces a much cleaner code like
$data = ['slug' => 'some slug', 'name' => 'some name'];
Category::create($data);

validation and duplicate update() in 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>';

Laravel validation rule `confirmed` message show on wrong field

For example you have 2 inputs: password and password_confirmed.
model
$rule = array (
'password' => 'min:4|confirmed',
'password_confirmed' => 'min:4',
);
If the user inputs the wrong password in the password_confirmed input, the validator sends the message to password so that the error message gets displayed with the password errors and not the password_confirmation errors.
How do I make the confirmation error go to the password_confirmation messages?
Well, this may not be a direct answer to your question, but will still give desired result.
Look into custom messages for your validation:
$messages = [
'password.confirmed' => 'Your passwords were mismatched',
'password_confirmation.min' => 'Your password must be at least 4 characters'
];
You should be able to get the desired message for any of your fields by utilizing this feature. Unless I am misunderstanding your issue?

Resources