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

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_-]+$/'],

Related

How to add validation for preventing duplicate level points in laravel query?

Here is a table named stages and below are the fields
I don't want to allow to add the same from and to points example, if points from 1 to 10 is already added means not allow them to add the same range, another one condition is don't allow to add in between points example in-between 1to 10 like 5 to 7 are also not allowed.
Tried laravel query
$isexist = Stage::whereBetween('from', [$request->from, $request->to])
->orWhereBetweenColumn('to','from', [$request->from, $request->to])->exists();
but it not satisfying all conditions.
Any one please help, thanks in advance.
you can validate the data like this
$validator = Validator::make($inputData, [
'label' => 'required|unique:tableName,label'
]);
if($validator->fails()){
//Throw validation errors
}
Or you can use
Model::query()->updateOrCreate(['label' => $request->label], [
'from' => $request->from,
'to' => $request->to
]);
Read more on unique validation reference: https://laravel.com/docs/9.x/validation#quick-writing-the-validation-logic

Laravel 5.5 form validation numeric/integer accepts non-digital characters like "+"

I want to validate the form data if it is just numeric/integer (as in just numbers). Based on Laravel's documentation there are two specific validators for that. But the problem I'm facing is that both the validators accept non-numeric characters such as "+" or "-".
numeric
The field under validation must have a numeric value.
integer
The field under validation must be numeric.
How can I make the validation to only accept numbers and not other non-numeric characters?
'main_telephone' => 'numeric',
'main_fax' => 'integer',
'direct_telephone' => 'integer',
'mobile' => 'integer',
Below is the screenshot
if anyone has come across this problem the best possible solution is to use regex. I do not know why I didn't think of this before.
'main_telephone' => 'integer|regex:/^[0-9]*$/',
'main_fax' => 'integer|regex:/^[0-9]*$/',
'direct_telephone' => 'integer|regex:/^[0-9]*$/',
'mobile' => 'integer|regex:/^[0-9]*$/',

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 - 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