Laravel validation rules custom message with second level of array - php

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.

Related

Skip the Validator Message argument in Laravel 8

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'
]);

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'
];

zend framework 3 Select tag validation

I am getting the error:
The input was not found in the haystack
After form post. Please see the selection tag code lines below:
// Add "roles" field
$this->add([
'type' => 'select',
'name' => 'roles',
'attributes' => [
'multiple' => 'multiple',
'options'=>$this->role_desc,
'inarrayvalidator' => false,
'class'=>'form-control'
],
'options' => [
'label' => 'Role(s)',
],
]);
// Add input for "roles" field
$inputFilter->add([
'class' => ArrayInput::class,
'name' => 'roles',
'required' => true,
'haystack'=>$this->role_ids,
'filters' => [
['name' => 'ToInt'],
],
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
['name'=>'InArray', 'options'=>['haystack'=>$this-
>role_ids]]
],
]);
The InArray seems to be validating well, lm just no sure what is bringing up the exception. Thank you in advance.
Actually , your issue is similar to link
To solve this, change your validators definition to:
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
[
'name' => 'Explode',
'options' => array(
'validator' => [
'name'=>'InArray',
'options'=> [
'haystack'=>$this->role_ids
]
]
)
]
],
Unfortunately, I do not think there is a "cleaner" way to do this.
Alternately, Maybe you could use the MultiCheckbox.

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',
],

Validation rules in cakephp 3

I have field "type" that need to be unique and rule for the field will be "active" == 1.
$validator
->notEmpty('type')
->add('type', [
'unique' => [
'rule' => ['validateUnique', ['scope' => ['aktivan' => '1']]],
'provider' => 'table',
'message' => 'Not unique']
]);
I try this but doesn't work. I am new to Cakephp and I need help with this.

Categories