Serialization of 'Closure' is not allowed Laravel Validation - php

I'm trying the laravel validator but getting the error in the title.
This is the part of my code:
What i'm doing wrong?
$data = Validator::make($request->all(),[
'number_1' => Rule::requiredIf(!$request->number_2 && !$request->number_3),
'number_2' => Rule::requiredIf(!$request->number_1 && !$request->number_3),
'number_3' => Rule::requiredIf(!$request->number_1 && !$request->number_2),
]);
I need those three numbers to be at least one required.

You can try this
$data = Validator::make($request->all(),[
'number_1' => 'required_without_all:number_2,number_3',
'number_2' => 'required_without_all:number_1,number_3',
'number_3' => 'required_without_all:number_1,number_3',
]);
Check this link for more info https://laravel.com/docs/8.x/validation#rule-required-without-all

Related

Manual Laravel Validation using array index (not keys)

A newbie here in terms of Laravel validation - I've searched and checked the manual but can't seem to find an answer to this. I'm using Laravel 8.0.
As per the Laravel manual I have created a manual validation to validate my array, it contains data similar to the below:
$row = array('John','Doe','john.doe#acme.com','Manager');
Because the array has no keys, I can't work out how to reference the array items using their index when creating the Validator, I've tried this:
$validatedRow = Validator::make($row, ['0' => 'required|max:2', '1' => 'required']);
$validatedRow = Validator::make($row, [0 => 'required|max:2', 1 => 'required']);
$validatedRow = Validator::make($row, [$row[0] => 'required|max:2', $row[0] => 'required']);
But no luck - does anyone have any suggestions?
Use Validator
use Illuminate\Support\Facades\Validator;
Then Update your code
$row = array('John','Doe','john.doe#acme.com','Manager');
$rule = [
'0' => 'required|string',
'1' => 'required',
'2' => 'required|email',
'3' => 'required'
];
$validator = Validator::make($row, $rule);
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()->all()], 422);
}else{
echo 'ok';
}

There is nothing happening when store data for Laravel API

I making laravel API where i can store a new data where the value in body raw json. but when i try to send request using post, i got nothing but the status is 200 OK. when i chek my mysql there is no data inputed.
So, what should i do?
mysql data
Laravel Controller, and API,
// function in controller
use App\Models\ChartAge;
class ChartController extends Controller
{
public function saveChart(Request $request)
{
$data = $request->validate([
'entity' => 'required|string|max:10',
'code' => 'required|string|max:10',
'year' => 'required|int|max:10',
'under_age_15' => 'required|string|max:50',
'age_15_64' => 'required|string|max:50',
'age_65_over' => 'required|string|max:50',
]);
$values = ChartAge::create($request);
return response()->json(
[
'status' => true,
'message' => "the videos has been favorites",
'data' => $values,
],
201
);
}
}
//in api.php
Route::post("charts", [ChartController::class, 'saveChart']);
and here is when i tried to send request using postman.
because there is no error, i don't know what's wrong??
First double check your ChartAge model, does it have $fillable or not?
and Edit your code:
From
$values = ChartAge::create($request);
To:
$values = ChartAge::create($request->all());
Hope this will be useful.
With validation:
$data = \Validator::make($request->all(),[
'entity' => 'required|string|max:10',
'code' => 'required|string|max:10',
'year' => 'required|int|max:10',
'under_age_15' => 'required|string|max:50',
'age_15_64' => 'required|string|max:50',
'age_65_over' => 'required|string|max:50',
]);
if($data-> fails()){
return back()->withErrors($data)->withInput();
}
$values = ChartAge::create($request->all());
Do you set fillable fields in your 'ChartAge' model?
protected $fillable = ['entity','code','year'...];
Do you try to test code with disabling validation?
Please try to put dd($request) in the first row of the controller code.
Method create expects a plain PHP array, not a Request object.

Laravel 5.2 validator: Validate Array request parameter, at least one value must be selected

I am having a form where i am having title, body, answers[][answer] and options[][option].
I want atleast one answer must be selected for the given question, for example:
i have ABC question and having 5 options for that question,now atleast one answer must be checked or all for given question.
Efforts
protected $rules = [
'title' => 'required|unique:contents|max:255',
'body' => 'required|min:10',
'type' => 'required',
'belongsto' => 'sometimes|required',
'options.*.option' => 'required|max:100',
'answers.*.answer' => 'required',
];
But this is not working. i want atleast one answer must be selected.
Please help me.
The problem is that on $_POST an array filled with empty strings will be passed if no answer is selected.
$answers[0][0] = ''
$answers[0][1] = ''
$answers[0][2] = ''
Hence the following will not work since array count will be greater than zero due to the empty strings:
$validator = Validator::make($request->all(), [
'answers.*' => 'required'
]);
The easiest way to solve this is to create a custom Validator rule by using Laravel's Validator::extend function.
Validator::extendImplicit('arrayRequireMin', function($attribute, $values, $parameters)
{
$countFilled = count(array_filter($values));
return ($countFilled >= $parameters[0]);
});
And then call it in your Validation request:
$validator = Validator::make($request->all(), [
'answers.*' => 'arrayRequireMin:1'
]);
The magic happens in array_filter() which removes all empty attributes from the array. Now you can set any minimum number of answers required.
Validator::extendImplicit() vs Validator::extend()
For a rule to run even when an attribute is empty, the rule must imply that the attribute is required. To create such an "implicit" extension, use the Validator::extendImplicit() method:
Laravel's validation docs
Try this,
'answer.0' => 'required'
it will help you. I think.

Laravel Validator fails due to array to string conversion

I'm trying to validate this input:
$values = [
'id' => $input['id'][$i],
'template_id' => $input['template_id'][$i],
'schedulable_id' => $id,
'schedulable_type' => $type,
'order_by' => $i
];
Against these rules found in my Schedule class:
public static $rules = [
'template_id' => 'required|integer|exists:templates,id',
'schedulable_id' => 'required|integer',
'schedulable_type' => 'required|in:Item,Order',
'order_by' => 'integer'
];
When I do the following, I always get an array to string conversion error in "/laravel/vendor/laravel/framework/src/Illuminate/Validation/Validator.php" on line 905:
$validator = Validator::make($values, Schedule::$rules);
if ($validator->fails()) {
$errors[$i] = $validator->messages();
continue;
}
Why would this be happening?
Just discovered I had Ardent's $forceEntityHydrationFromInput = true and my input cannot be pulled directly from Input for validation purposes due to the fact that it is submitted as an array of partially referenced values.
To fix this, change to $forceEntityHydrationFromInput = false and use standard input validation procedure instead of relying on Ardent's magic.
Sometimes clever packages are too clever.

Why validation triggers error in Laravel?

Faced the problem while using Laravel 4 validation. Here's the code:
$validator = Validator::make(
array(
'surname' => ['Laravel'],
),
array(
'surname' => 'integer|alpha_dash'
)
);
$validator->passes();
var_dump($validator->failed());
It causes error: Error: preg_match() expects parameter 2 to be string, array given
Lets suppose surname comes from user and it can be array or string.
I have two questions:
Why alpha_dash causes error instead of normal validation error?
Why validation continues to 'alpha_dash' rule after we got FALSE on 'integer' rule? Is this a bug?
What I just did to test arrays:
Created some fields the way you are doing:
<input type="text" name="user[surname][0]">
<input type="text" name="user[surname][1]">
And validated one of them:
$validator = \Validator::make(
Input::all(),
array('user.surname.0' => 'required|min:5')
);
var_dump($validator->passes());
Then I just did it manually:
$validator = \Validator::make(
array(
'user' => ['surname' => [ 0 => 'Laravel'] ],
),
array('user.surname.0' => 'required|min:5')
);
And it worked on both for me.
If you need to analyse something Laravel doesn't provide, you can extend the Validator by doing:
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
EDIT
But, yeah, there is a bug on it, if you do:
$validator = \Validator::make(
array(
'user' => ['surname' => [ 0 => 'Laravel'] ],
),
array('user.surname.0' => 'integer|alpha_dash')
);
It will give you a
"Error: preg_match() expects parameter 2 to be string, array given".
Here's the Laravel issue posted by the OP: https://github.com/laravel/laravel/issues/2457.

Categories