Skip the Validator Message argument in Laravel 8 - php

I'm trying to pass custom attributes as the fourth argument to the Validator. But, I don't want to add any custom messages. In order to do that, I need to skip the messages argument.
$validator = Validator::make($request->all(),[
'firmType' => 'required',
'firmName' => 'required|max:255',
'firmCode' => 'required'
],$message,[
'firmType' => 'Firm Type',
'firmName' => 'Firm Name',
'firmCode' => 'Firm Code'
]);
I need to skip to provide $message array as I don't have any custom messages. Appreciate it if anyone could help with this.

I personally think this is one of the limitations of using Validator::make(..).
I would suggest you make a seperate Form Request (https://laravel.com/docs/8.x/validation#creating-form-requests) where you supply rules and attributes as methods.
public function rules()
{
return [
'firmType' => 'required',
'firmName' => 'required|max:255',
'firmCode' => 'required'
];
}
public function attributes()
{
return [
'firmType' => 'Firm Type',
'firmName' => 'Firm Name',
'firmCode' => 'Firm Code'
];
}

The below is the idea that I used to solve. Need to Pass an empty array for $message argument
$validator = Validator::make($request->all(),[
'firmType' => 'required',
'firmName' => 'required|max:255',
'firmCode' => 'required'
],[],[
'firmType' => 'Firm Type',
'firmName' => 'Firm Name',
'firmCode' => 'Firm Code'
]);

Related

Laravel validation rules custom message with second level of array

Here my code :
$rules = [
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'unit' => 'required|in:piece,kg,m',
'price_type' =>'required|string',
'service' => [
'string',
'required',
Rule::in($services_ids->all()),
],
'facility' => [
'string',
'required',
Rule::in($facilities_ids->all()),
],
'conciergeries' => [
'array',
'required',
Rule::in($conciergeries_ids->all()),
],
];
$custom_messages = [
'required' => 'Vous devez sélectionner un(e) :attribute.'
];
$validated = request()->validate($rules, $custom_messages);
The problem is that my custom_messages only works with 'name', 'price', 'unit', 'price_type' but not with 'service', 'facility' and 'conciergeries'.
Questions :
How to apply my custom messages with 'service', 'facility' and 'conciergeries' too ?
How to create a custom message for specifically one field ?
Thank's !
You just need to specify for which field you want to change the message
Try it like:-
$custom_messages = [
'service.required' => 'Your custom message for required service',
'service.string' => 'Your custom message of service should be string',];
And same process for facility and conciergeries.

Laravel 6: How to using custome validation message in field

I am wondering if I can create a custom validation message for a specific field.
i have tried using this
$rules = [
'nama' =>'required',
'spesialis' =>'required',
'alamat' => 'required',
'telp' =>'required',
'tanggalMulai' => 'required'
];
$message=[
'required' => '* :attribute Harus Diisi'
];
$this->validate($request,$rules,$message);
Dokter::create([
'nama' => $request->nama,
'spesialis' =>$request->spesialis,
'alamat' => $request->alamat,
'telp' => $request->telp,
'tanggalMulai' => $request->tglMulai
]);
return redirect()->route('dokter');
but after i using this, i can't save my data
This is how i solve the problem
$message=[
'required' => '* :attribute harus diisi',
'min' =>'*:attribute minimal :min karakter'
];
$validatedData = $request->validate([
'nama' =>'required',
'spesialis' =>'required',
'alamat' => 'required',
'telp' =>'required|numeric|min:9',
'tanggalMulai' => 'required|date'
],$message);
Dokter::create($validatedData,$message);
You've to pass custom messages per particular field.
$message=[
'nama.required' => 'name field is required',
'spesialis.required' => 'spesialis field is required'
];

laravel validation error when trying to update

When I try to update a post if I dont change value of slug , laravel return this error:
The slug has already been taken.
Here is my validation:
$this->validate($request, [
'name' => 'required',
'content' => 'required',
'excerpt' => 'required',
'type' => 'required',
'slug' => 'required | unique:providers'
]);
when you're updating records and there is unique validation is added. You should pass the id of which record you're inserting.
While Update
'slug' => 'required | unique:providers,slug,'.$providerId
While insert
'slug' => 'required | unique:providers'
You need to add an except rule to your unique case to exclude the current ID from check:
$this->validate($request, [
'name' => 'required',
'content' => 'required',
'excerpt' => 'required',
'type' => 'required',
'slug' => 'required|unique:providers,id,' . $provider->id
]);
Make the column and the id that you pass to use your table's data. I am just giving you an idea on what you need to add.
You need to tell the validator to do an exception for the current entity you're updating.
$this->validate($request, [
'name' => 'required',
'content' => 'required',
'excerpt' => 'required',
'type' => 'required',
'slug' => 'required|unique:providers,slug,'.$providerId
]);
When adding:
use Illuminate\Support\Str;
.
.
$slug = Str::slug($request->name, '-');
$exists = Provider::where('slug', $slug)->first();
if($exists) {
$slug = $slug.'-'.rand(1000,9999);
}
When updating:
$request->validate([
'slug' => 'required|unique:providers,slug,'.$provider_id.',id',
]);

laravel 5.2 set attribute name

How i set custom the attributte names in laravel 5.2 I already try this code, but doesn't work:
$attNames = array(
'code' => 'Número',
'contributor' => 'Nº Contribuinte',
'create_date' => 'Data criação',
'address' => 'Morada',
'zip_code' => 'Cod. Postal',
'city' => 'Localidade',
'email' => 'E-mail',
'phone_number' => 'Telefone',
'note' => 'Observações',
);
$validator = Validator::make($client, $this->rules,[],$attNames);
$validator->setAttributeNames($attNames);
if ($validator->fails()) {
// send back to the page with the input data and errors
$errors = $validator->messages();
return Redirect::to('/client/create')->withInput()->withErrors($errors);
}
You have passed wrong arguments to Validator::make.
You can pass only three arguments.
As per Documentation,
If needed, you may use custom error messages for validation instead of
the defaults. There are several ways to specify custom messages.
First, you may pass the custom messages as the third argument to the
Validator::make method.
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
I figure out.
controller:
use Validator;
(...)
$attName=array(
'code' => trans('validation.code'),
'contributor' => trans('validation.contributor'),
'create_date' => trans('validation.create_date'),
'address' => trans('validation.address'),
'zip_code' => trans('validation.zip_code'),
'city' => trans('validation.city'),
'email' => trans('validation.email'),
'phone_number' => trans('validation.phone_number'),
'note' => trans('validation.note'),
);
$validator = Validator::make($client, $this->rules, [], $attNames);
validation.php:
'attributes' => [
'code' => 'número',
'contributor' => 'nº contribuinte',
'create_date' => 'data criação',
'address' => 'morada',
'zip_code' => 'cod. postal',
'city' => 'localidade',
'email' => 'e-mail',
'phone_number' => 'telefone',
'note' => 'observações',
],

Laravel 'sometimes' validator fails with nested arrays

Am using the Laravel Validator class to do some basic validation on an array.
My array :
$employee['name']='name';
$employee['address']='address';
$employee['department']['department_name']='deptname';
$employee['department']['department_address']='deptaddress';
I have the validation rules as below:
$rules = array(
'name'=> 'required',
'address' => 'required',
'department.department_name' => 'sometimes|required'
)
And the custom messages as below :
$messages = array(
'name.required' => 'Employee Name is required',
'address.required' => 'Address is required'
'department.department_name.required' => 'Department name is required'
)
I will use Validator::make($employee, $rules, $messages);
As per my rules, department_name should be validated if and only if it is present in the array. But currently the Validator is not validating department_name when its present and blank. Any ideas what I might be doing wrong?
You are going a bit wrong here see the docs here
it is mentioned there If I have $photos['profile'] then my validation will go like this
$validator = Validator::make($request->all(), [
'photos.profile' => 'required|image',
]);
From the above example, In your case it should be like this
$rules = array(
'name'=> 'required',
'address' => 'required',
'employee.department.department_name' => 'sometimes|required'
)
Since you have array like this $employee['department']['department_name']
So does the $message will go like this
$messages = array(
'name.required' => 'Employee Name is required',
'address.required' => 'Address is required'
'employee.department.department_name.required' => 'Department name is required'
)

Categories