In Laravel, we can do validation on array like
$this->validate($request, [
'users.*.name' => 'bail|nullable|min:2',
'users.*.username'=>'bail|nullable|min:4|unique:t0101_user,username,' .$xx
'users.*.password' => 'bail|nullable|min:6'
], $this->messages());
In this scenario, what should i passed to the xx ?
I will need something like
users.*.id
Thank you,
(Laravel version 5.4)
Laravel Unique Validation Rule
Documentation Link
You need to manually create a validator using the unique rule
use Illuminate\Validation\Rule;
Validator::make( $data, [
'users.*.username' => [
'bail', 'nullable', 'min:4', Rule::unique( 't0101_user', 'username' )->ignore( 'users.*.id' )
]
]);
Related
I have an array of values that I send together with other fields in a form to laravel.
The array contains a role_id field and a status field, the status can be I (Insert) U (update) D (Delete). When I validate the values in the array I want it to skip the ones where the status equals D. Otherwise I want it to validate.
private function checkValuesUpdate($userid = null)
{
return Request::validate(
[
'displayname' => 'required',
'username' => 'required',
'email' => ['nullable', 'unique:contacts,email' . (is_null($userid ) ? '' : (',' . $userid ))],
'roles.*.role_id' => ['exclude_if:roles.*.status,D', 'required']
]
);
}
I can't seem to get it to work. I've been searching all over the place for functional code as well as the Laravel documentation. But no luck so far. Has anybody ever done this?
Your problem comes from a misunderstanding of the exclude_if rule. It doesn't exclude the value from validation, it only excludes the value from the returned data. So it would not be included if you ran request()->validated() to get the validated input values.
According to the documentation, you can use validation rules with array/star notation so using the required_unless rule might be a better approach. (Note there's also more concise code to replace the old unique rule, and I've added a rule to check contents of the role status.)
$rules = [
'displayname' => 'required',
'username' => 'required',
'email' => [
'nullable',
Rule::unique("contacts")->ignore($userid ?? 0)
],
'roles' => 'array',
'roles.*.status' => 'in:I,U,D',
'roles.*.role_id' => ['required_unless:roles.*.status,D']
];
Laravel throw an exception at $validator->fails() call.
Ok, I just want to create a stateless register method in ApiController.php with Laravel 5.7.
I used the Validator facade to check the sent data.
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique',
'password' => 'required',
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()],
Response::HTTP_UNAUTHORIZED);
}
But, when I use xdebug, I see something strange. The fails methods seems throw an exception.
Laravel send an error HTML page with title:
Validation rule unique requires at least 1 parameters.
The route is used in api.php
Route::post('register', 'Api\UserController#register');
Do you have an explanation for this?
Thx for reading.
The syntax for unique rule is unique:table,column,except,idColumn.
So i changed it for you to use the users table.
If you don't want to use the users table change the users part behind unique:
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required',
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()],
Response::HTTP_UNAUTHORIZED);
}
For more information on the unique rule see this: https://laravel.com/docs/5.7/validation#rule-unique
In your API request add the following header:
...
Accept: application/json // <-----
This will tell Laravel that you want a response in a json format.
Note: this is different to the Content-type: application/json. The later indicates the format of the data tha is being sent in the body.
Is there a way to set a validation on multiple inputs with similar name? For ex -
public function rules()
{
return [
'zone1' => 'required|numeric',
'zone2' => 'required|numeric',
'zone3' => 'required|numeric',
];
}
Can I do something like 'zone*' => 'required|numeric'
You can use an asterisk as a wildcard but it may not be a great idea. With a rule like 'zone*' => 'required|numeric' as long as there's a single value that matches the condition the request will pass validation. For example, if a request has a valid value for zone2 but zone1 and zone3 are missing or non-numeric the validation will still pass
I send an array to a REST API. How can I add a rule for the array?
Also I want to add field_name_id, field_input_type and field_caption as required fields.
I don't know how can I access the array in Laravel rules. Can someone help me?
$rules = [
'name' => 'required',
'forms' => 'array'
]
Laravel uses dot notation to validate arrays and it's nested fields.
$rules = [
'forms.field_name_id' => 'required',
'forms.field_input_type'=> 'required',
'forms.field_caption' => 'required',
]
You can also validate each value within the array. For example, If you want the caption to be unique:
$rules = [
'forms.*.field_caption' => 'unique:captions,caption',
]
Here are the docs for more information on how to use them
I wanted to validate an 'account_id' only if 'needs_login' is present. I did this:
$rules = [
'account_id' => ['required_with:needs_login','custom_validation']
];
But it doesn't work, because if needs_login field is not present but account_id has some value, then it tries to do the 'custom_validation'.
I also tried to put the 'sometimes' parameter
$rules = [
'account_id' => ['required_with:needs_login', 'sometimes', 'custom_validation']
];
but it didn't work.
Any ideas?
P.S.: Remember that I wanted to validate the account_id only if needs_login is present, not to check if account_id is present if needs_login does.
Have you tried required_if?
$rules = [
'account_id' => ['required_if:needs_login,1']
];
Something like this works for Laravel 5 if you are going the 'sometimes' route. Perhaps you can adapt for L4? Looks like it's the same in the Docs.
$validation = Validator::make($formData, [
'some_form_item' => 'rule_1|rule_2'
]
$validation->sometimes('account_id', 'required', function($input){
return $input->needs_login == true;
});