Laravel validator custom messages doesn't work with nested attributes - laravel

I try to perform a manual validation according to the documentation.
There is my rules:
$rules = [
"firstname" => ['required'],
"coord.address" => ['required'],
];
And there is my custom messages:
$messages = [
"firstname.required" => "Custom message for firstname",
"coord.address.required" => "Custom message for address",
];
My call for perform the validation
$errorMessages = Validator::make($attributes, $rules, $messages)->errors()->messages();
And there is my messages form the validator:
[
"firstname" => "Custom message for firstname",
"coord.address" => "The field coord.address is required",
]
What im doing wrong ?
I tried to use an array of messages like this:
$messages = [
"coord" => [
"address.required" => "Custom message",
]
];
Or like this:
$messages = [
"coord" => [
"address" => [
"required" => "Custom message"
]
]
];
But same problem.

Related

How can create a test in laravel for check load relationships

public function test_show_role_should_return_actions()
{
$this->actingAs($this->user, 'api');
$this->withHeaders(['Accept' => 'application/json',])
->get(route('roles.show', ['role' => $this->role->id]))
->assertJsonStructure([
"data" => [
"name",
"actions" => [
"name",
"code"
]
]
]);
}
when I run the test,I have this error:
Tests\Feature\Role\RoleTest::test_show_role_should_return_actions
Failed asserting that an array has the key 'name'.
Even when I remove the "name",I get an error for the "actions" key.
public function test_show_role_should_return_actions()
{
$this->actingAs($this->user, 'api');
$this->getJson(route('roles.show', ['role' => $this->role->id]))
->assertJsonStructure([
'data' => [
'*' => [
'name',
'actions' => [
'*' => [
'name',
'code'
]
]
]
]
]);
}

How to set required_if in laravel 7

I have two textboxes color_name[] and color_code[],
How to put required_if? If the color code is not empty, the color name is required.
'color_name' => 'required_if:color_code',
error
Validation rule required_if requires at least 2 parameters.
$validator = Validator::make($request->all(), [
'color_name' => 'required_with:color_code',
]);
if ($validator->fails()) {
return $validator->errors()->first();
}
Use this to solve the problem.
For array please re shape your array into this
array:2 [
0 => array:2 [
"color_name" => "red"
"color_code" => "#77"
]
1 => array:2 [
"color_name" => "blue"
"color_code" => "#88"
]
]
$validator = Validator::make($request->all(), [
'color'=>'array',
'color.*.color_name' => 'required_with:color.*.color_code',
]);

Laravel: Validate an array of strings

How to validate that fruits from a given $list are in the allowed list ?
$attributes [
"fruits" => ['nullable', 'string', 'in:apple,banana,orange'],
];
$list = [
"fruits" => [
"apple",
"orange"
]
];
$validator = Illuminate\Support\Facades\Validator::make($list, $attributes);
you can validate each item in this array individually:
$attributes = [
"fruits.*" => ['nullable', 'string', Rule::in(['apple','banana','orange'])],
];

Format validation error message as nested associative array

I have a Json object of data as shown below
{
"name": "something",
"location": {
"city": "some where",
"country": "some where",
}
}
Rule used to validate Request is
[
'name' => 'required',
'location.city' => 'required',
'location.country' => 'required'
]
Which returns error message like
{
"name": [
"The name field is required."
],
"location.city": [
"The location.city field is required."
],
"location.county": [
"The location.country field is required."
]
}
How can I format error message as a nested array like the Request.
{
"name": [
"The name field is required."
],
"location": {
"city": [
"The city field is required"
],
"country": [
"The country field is required"
]
}
}
Any default methods available ?
I am using Illuminate\Foundation\Http\FormRequest
In your case , you need to build the error message yourself. you can still use the default messages in the ressources/lang/en/validation messages file.
$validator = Validator::make($request->all(), [
'name' => 'required',
'location.city' => 'required',
'location.country' => 'required'
]);
if ($validator->fails()) {
return response()->json($yourOwnFormat,422);
//you can use $validator->errors() to build it
}
For those who looking for the solution This is how I implemented
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
class UserStoreRequest extends FormRequest
{
public function rules()
{
return [
'name' => 'required',
'location.city' => 'required'
'location.country' => 'required'
];
}
public function attributes()
{
return [
'location.city' => 'City'
'location.country' => 'Country'
];
}
protected function failedValidation(Validator $validator)
{
$errors = $validator->errors()->getMessages();
$errors_formated = array();
foreach ($errors as $key => $value) {
array_set($errors_formated, $key, $value);
}
throw new HttpResponseException(response()->json(['error' => $errors_formated], 422));
}
}
The result of $validator->errors()->getMessages() is just like array_dot() helper function result. So I did the Opposite of array_dot(), also altered my attribute name into pretty name
Is it? Laravel Documentation- Customizing the Error Messages
public function messages()
{
return [
'location.city' => 'The city field is required',
'location.county' => 'The county field is required',
];
}

Unknown key for a VALUE_STRING in [index]

I'm trying to find a way where I can build my $params array using the native syntax to search for a post which name is equal to 'test':
$params = [
"index" => "post_index",
"type" => "post",
'body' => [
'query' => [
'match' => [
'name' => 'test'
]
]
]
];
$result = $finder->find($params);
Unfortunately I'm getting this error:
Unknown key for a VALUE_STRING in [index].
I know that I can use some Elastic\Query\Match to build my query.
Just I fixed my $params array this way :
$q = [
'query' => [
'match' => [
'title' => 'test'
]
]
];

Resources