i want to make a laravel validator that validates the the fields inside an un-named array ( 0,1,2,3 ) that is inside an array
so my array is like
array [ //the form data
"items" => array:2 [ //the main array i want to validate
0 => array:2 [ // the inner array that i want to validate its data
"id" => "1"
"quantity" => "1000"
]
1 => array:2 [
"id" => "1"
"quantity" => "1000"
]
// other fields of the form,
]
]
so what i want is something like
$validator = Validator::make($request->all(), [
'items.*.id' => 'required' //notice the star *
]);
Laravel 5.2
The syntax in the question is now supported
http://laravel.com/docs/master/validation#validating-arrays
Laravel 5.1
First create the validator with all of your other rules. Use the array rule for items
$validator = Validator::make($request->all(), [
'items' => 'array',
// your other rules here
]);
Then use the Validator each method to apply a set of rules to every item in the items array.
$validator->each('items', [
'id' => 'required',
'quantity' => 'min:0',
]);
This will automatically set these rules for you...
items.*.id => required
items.*.quantity => min:0
https://github.com/laravel/framework/blob/5.1/src/Illuminate/Validation/Validator.php#L261
You could simply do something like that:
$rules = [];
for($i = 0; $i < 10; $i++) {
$rules["items.$i.id"] = "required";
}
$validator = \Validator::make($request->all(), $rules);
Related
I try to query my collection with only one query and 3 potentials search method:
fulltext search
classic search
search regex
This 3 matches can be executed at the same time or just one of them.
The fulltext search is the first stage pipeline as we know. Does this fulltext search can be optional in my aggregate? Because if my default value of search is "", my query returns any data. And I need data to perform my other optionals matches.
Here is my Laravel 8 controller :
Product::raw(function ($collection) use($filters, $fullText, $likeKey, $likeValue){
return $collection->aggregate([
[
'$match' =>
[
'$text' =>['$search' => $fullText],
],
],
[
'$match' => $filters
],
[
'$match' =>
[
$likeKey =>
[
'$regex' => $likeValue,
'$options' => "i"
]
]
],
[
'$addFields' =>
[
'avgReviews' => ['$avg' => '$reviews.ranking'],
'price' => ['$min' => '$variants.price'],
'equipmentsList' => [
'$reduce' => [
'input' => '$equipments.list.list',
'initialValue' => [],
'in' =>
[
'$concatArrays' => [
'$$value',
'$$this'
]
]
]
]
],
]
]);
})
->when($operations, function($products) use ($operations){
foreach($operations as $key => $operation){
return $products
->where($operation[0],$operation[1],$operation[2]);
}
})
->forPage($page,$limit)
->sortBy($sortBy, SORT_REGULAR, $order == 'desc')
->values();
$filters is an array and works fine when it's the only one match. But if I want to use $filters without $text, it returns any data. And with the third match, nothing works. Can somebody help me with this?
Currently I have set of array.
and
I can easily insert these data to my database using Laravel without doing any validations
here's the sample array
CODE:
$excel1 = Importer::make('Excel');
$excel1->hasHeader(true);
$excel1->load($savePath.$fileName);
$excel1->setSheet(2);
$collection1 = $excel1->getCollection();
$arr1 = json_decode($collection1,true);
foreach ($arr1 as $row1) {
$insert_data1[] = array(
'projCode' => $projCode,
'emp_id' => $row1['company_id'],
'type' => 'EMP',
'deleted' => 0,
'by_id' => auth()->user()->id,
'updated_by' => auth()->user()->name,
'created_at' => now(),
'updated_at' => now(),
);
}
dd($insert_data1);
OUTPUT:
and I'm using this code to insert these data to my table
DB::table('tbl_emp_proj')->insert($insert_data1);
and this works fine but the problem is,
I'm trying to validate if emp_id exists or not in my users table
Here's my users table
The value of emp_id from array should check if it already exists in my users using company_id field from users. How can I validate it if $insert_data1 is an array and should be check if it exists on database?
UPDATE
currently i have this validator and I tried to add up the $Insert_data1 but gives me undefined var for $insert_data1.
$validator = Validator::make(
[
'file' => $request->file,
'extension' => strtolower($request->file->getClientOriginalExtension()),
],
[
'file' => 'required|max:5000',
'extension' => 'required|in:,csv,xlsx,xls',
],
$insert_data1,
[
'*.emp_id' => "required|exists:users,company_id",
]
);
You can use Laravel Validator to validate any arrays as if its a request input.
use Illuminate\Support\Facades\Validator;
$validator = Validator::make(
$insert_data1,
[
'*.emp_id' => "required|integer|exists:users,company_id",
]
);
EDIT:
You can receive error messages and error items with the validator APIs.
$failed = $validator->fails(); //boolean
$errors = $validator->errors();
$validated = $validator->validated();
$invalid = $validator->invalid();
I have an array, which looks like this:
array:3 [▼
"field" => array:2 [▼
0 => "fromName"
1 => "from"
]
"operator" => array:2 [▼
0 => "="
1 => "="
]
"value" => array:2 [▼
0 => "Oliver"
1 => "oliver#mywebsite.com"
]
]
I am trying to save the above array, into my database table called email_rules:
Below is my code.
StreamEmailRulesController.php:
public function store(Stream $stream)
{
//Validate the request.
//Validate the request.
$attributes = request()->validate([
'field' => 'required|array|min:1',
'field.*' => [
'required', 'string',
Rule::in(['fromName', 'from']),
],
'operator' => 'required|array|min:1',
'operator.*' => [
'required', 'string',
Rule::in(['=', '!=', 'matches']),
],
'value' => 'required|array|min:1',
'value.*' => 'required|string|min:3|max:255',
]);
//Add the document to the database.
$stream->addRules($attributes);
//Return back.
return redirect()->back();
}
Now the $stream->addRules() function is responsible for saving the data to the database.
Stream.php:
/**
* A stream can have many rules.
*/
public function rules()
{
return $this->hasMany(EmailRule::class);
}
/**
* Add Email Rules(s) to the stream
*
* #return Illuminate\Database\Eloquent\Model
*/
public function addRules(array $attributes)
{
return $this->rules()->create($attributes);
}
Now, above does not work. I get below error:
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, int given,
What am I doing wrong?
If you dump $attributes you may be getting an int (bool) as a pass or fail or even json, depending on what's going in, from the validation. This might just be a matter of changing syntax from
$attributes = request()->validate([...
to
$attributes= $this->validate(request(), [...
I believe your issue is that you're trying to save an array as a singular value. IE those attributes need to be iterated over to create a new set of rules for each one, instead. Normally, I'd expect to see the array ready to create individual objects. In this case, though it looks like it is structured to create individual fields (field, operator, value), so looping through those may not do what you wish either -- it provides multiple fields to the create construct, rather than a full set of object params for a new rule(). I think Laravel is hinting that you may wish to change your request/return structure to match the model format.
I think it could be the array structure. Can you modify the array to?:
[
[
"field" => "fromName",
"operator" => "=",
"value" => "Oliver"
],
[
"field" => "from",
"operator" => "=",
"value" => "oliver#mywebsite.com"
],
]
EDIT:
In the Controller add a loop like this:
...
foreach ($attributes as $key => $value) {
foreach ($value as $k => $v) {
$data [$k][$key] = $v;
}
}
//Add the document to the database.
$stream->addRules($data);
The problem was that Laravels create or createMany expect an array with key => pair values, where the key corresponds to the database columns.
This article from Adam Wathan helped me out a lot.
This is what I ended up doing:
$requestData = collect(request()->only('field', 'operator', 'value'));
$rules = $requestData->transpose()->map(function ($ruleData) {
return new EmailRule([
'field' => $ruleData[0],
'operator' => $ruleData[1],
'value' => $ruleData[2],
]);
})->toArray();
//Add the rules to the database.
$stream->addRules($rules);
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'm currently working on my Edit form in my Laravel application.
I request all the inputs from a form submit. I got :
array:6 [▼
"_method" => "PUT"
"_token" => "iWyonRYFjF15wK8fVXJiTkX09YSPmXukyGbBcHRA"
"phone" => "9786770863"
"email" => "test#sites.com1"
"address" => "23 School St Lowell MA 01851"
"$id" => "1"
]
My goal is to only validate only : phone, email, and address.
I've tried
$validator = Validator::make(
['phone' => 'max:20'],
['email' => 'required|email|unique:users,email,'. $id ],
['address' => 'max:255']
);
// dd(Input::get('email')); // HERE <------ I got the email to display
if ( $validator->fails()) {
return Redirect::to('user/profile/'. $id )->withErrors($validator)->withInput();
} else {
$user = User::findOrFail($id);
$user->phone = Input::get('phone');
$user->email = Input::get('email');
$user->address = Input::get('address');
$user->save();
It keep failing on me and say that
The email field is required.
But if I recall correctly the email field is there.
How can validate only a certain fields in php Laravel 5 ?
It should be :
$validator = Validator::make($input, [
'phone' => 'max:20',
'email' => 'required|email|unique:users,email,'. $id ,
'address' => 'max:255']
);
It thinks you are passing the first line as the data to check, and the second line as the rules for your validation. It doesn't find an email key so it tells you it is required.
Your Validator::make() method call is a bit off.
When using this function, the first parameter is the array of data to validate (your request data), and the second parameter is your array of rules.
Your current code has you passing in three parameters. It is treating ['phone' => 'max:20'] as your data to validate, ['email' => 'required|email|unique:users,email,'. $id ], as your rule, and then ['address' => 'max:255'] as your messages array.
It should be something like this:
$validator = Validator::make(
Input::all(),
[
'phone' => 'max:20',
'email' => 'required|email|unique:users,email,'. $id,
'address' => 'max:255'
]
);