from request I get an array like this:
'array' => [
0 => ['id' => 1,'val' => 2],
1 => ['id' => 1,'val' => 2]
]
I need to validate it so all ids of array will be unique.
right now I try this validation rule:
'array.*.id' => 'different:array.*.id'
but it will check current array with current array so result will be like
The array.0.id and array.0.id must be different.
You should use distinct rule:
'array.*.id' => 'distinct'
Related
I have a trouble validating an array members in validation for Laravel. For array itself it passes, but for elements themselves, it fails.
Open json in console, shows structure of data being sent. It's clearly a integer. Since it's associative array, not sure how it even finds it's value as show in error handling red rectangle on the bottom of the page.
Here is my validation logic:
$validation = Validator::make(
$request->all(),
[
'load_place' => 'required|max:255',
"unload_place" => 'required|max:255',
"comment" => 'required|max:255',
'time_in' => 'required',
'time_out' => 'required',
"vehicles" => 'required|array',
"vehicles.*" => "required|integer",
'operator_id' => 'required|integer',
'sec_id' => 'required|integer'
]
);
And here is how it's returned:
if($validation->fails()){
$response = array(
"message" => "Failed",
"errors" => $errors,
"test" => $request->vehicles[0]
);
return response()->json($response);
}
That request[0], is that number at the bottom.
Edit1:
Keys in that marked array, are set on frontend, they are not not predefined. They can be whatever.
Change "vehicles.*" => "required|integer", to "vehicles.*.vehicle_id" => "required|integer", and it should work.
Keys is set on frontend, they are not not predefined. That what i am trying to say.
Try to replace "vehicles.*" => "required|integer", with new Rule::forEach rule for nested array data - guess it should work.
"vehicles.*" => Rule::forEach(function($value, $attribute) {
return [
Rule::integer()->min(1) //min(1) as eg if it's for an id field which cannot be 0
];
}),
This is my collection data
[
0 => object(App\Model\Entity\SummaryCoin) id:0 { 'user_id' => (int) 2
'total_sum_coin' => (float) 3 },
1 => object(App\Model\Entity\SummaryCoin) id:1 { 'user_id' => (int) 1
'total_sum_coin' => (float) 33 },
]
How can I get the index where user_id = 1
Using first match I am able to get user 1 new collection
$sumOfUserCoins = $collection->firstMatch([
'user_id' => 1
]);
How I will get the array index 1 that user have ?
There is no specific method for that, you'll have to be a little creative to obtain that information, for example match all instead so that you get a collection back, take everything away expect for the first entry, and convert it to an array that keeps the keys, from which you can then get that information:
$userCoins = $collection
->match(['user_id' => 1])
->take(1)
->toArray();
$index = key($userCoins);
$coin = current($userCoins);
Here is what I put in my Symfony session
$this->session->set('teams', [
'team_1' => ['MyTeamNameA' => ['player-1' => $safe['team-1-player-1'], 'player-2' => $safe['team-1-player-2'], 'player-3' => $safe['team-1-player-3'], 'player-4' => $safe['team-1-player-4'], 'points' => 0]],
'team_2' => ['MyTeamNameB' => ['player-1' => $safe['team-2-player-1'], 'player-2' => $safe['team-2-player-2'], 'player-3' => $safe['team-2-player-3'], 'player-4' => $safe['team-2-player-4'], 'points' => 0]],
]);
And now, in my Twig, I want to retrieve my team_1's name, for example, I did this:
app.session.get('teams')['team_1']
It doesn't work, but, if I do a dump of this last piece of code, I get this result pictured below:
I feel that i'm close to the answer, and yet so far.
Since keys will return you a simple array, then the keys are integer from 0 to the (array | length) - 1, to express it in Twig.
Note that this is actually the same behaviour as PHP, when you define an array like
['foo', 'bar', 'baz']
that would be strictly equivalent to
[0 => 'foo', 1 => 'bar', 2 => 'baz']
So in your case, since you only have one element in the array you can use array[0] or array.0.
All together, this would work:
{{ (app.session.get('teams').team_1 | keys).0 }}
Could be tested from https://twigfiddle.com/b6iy8i
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am building an installer for perl modules we require at work. After all of the dependencies are installed, I need to check if the module is working properly "use This::Module". The problem is, all of the modules are being installed within a certain order.
The hash looks like...
my %modules=(
0 => {'name' => '/root/mods/CGI/perl-FCGI-0.74-8.amzn2.0.2.x86_64.rpm', 'method' => 'rpm', 'status'=>'pending'},
1 => {'name' => '/root/mods/CGI/perl-CGI-3.63-4.amzn2.noarch.rpm', 'method' => 'rpm','status'=>'done', 'validate' => ['CGI','CGI::Carp']},
2 => {'name' => '/root/mods/Digest/perl-Digest-1.17-245.amzn2.noarch.rpm', 'method' => 'rpm','status'=>'pending'},
3 => {'name' => '/root/mods/Digest/perl-Digest-MD5-2.52-3.amzn2.0.2.x86_64.rpm', 'method' => 'rpm','status'=>'done', 'validate' => ['Digest::MD5']},
4 => {'name' => '/root/mods/HTTP/perl-Business-ISBN-Data-20120719.001-2.el7.noarch.rpm','method' => 'rpm','status'=>'pending'},
5 => {'name' => '/root/mods/HTTP/perl-Data-Dumper-2.145-3.el7.x86_64.rpm','method' => 'rpm','status'=>'pending'},
6 => {'name' => '/root/mods/HTTP/perl-Business-ISBN-2.06-2.el7.noarch.rpm','method' => 'rpm','status'=>'done'}, 'validate' => ['HTTP::Request::Common']},
Each newline is the start of a new module. Once one the 'status' => 'done' I need to access the modules within 'validate'. This is an array because there are cases where there are multiple modules tied to one install sequence.
How can I loop through and return each array element by itself?
'validate' => {['CGI','CGI::Carp']}},
You don't have arrays.
The value of validate is between { and } so it is a hashref.
The arrayref is the first entry in the hash, so it gets converted to a string to be used as a key.
You end up with something like:
{
'name' => '/root/mods/CGI/perl-CGI-3.63-4.amzn2.noarch.rpm',
'method' => 'rpm',
'status' => 'done',
'validate' => {
'ARRAY(0x7f9ab601c4e0)' => undef
}
};
Make sure you use strict; and use warnings;. It would have alerted you to this:
Odd number of elements in anonymous hash at Untitled.pl line 9.
You need to fix your data structure. If you want an array, then put it in an array.
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use Data::Dumper;
my %modules = (
0 => {
'name' => '/root/mods/CGI/perl-FCGI-0.74-8.amzn2.0.2.x86_64.rpm',
'method' => 'rpm',
'status' => 'pending'
},
1 => {
'name' => '/root/mods/CGI/perl-CGI-3.63-4.amzn2.noarch.rpm',
'method' => 'rpm',
'status' => 'done',
'validate' => [ 'CGI', 'CGI::Carp' ]
},
);
Then you can access the arrayref:
my $arrayref = $modules{1}->{validate};
and loop over it:
foreach my $value (#$arrayref) {
say $value
}
I have a collection in mongo which has array field. When I query that collection I want to return documents whose elements in the array field represent a subset of the array I'm comparing with. So all array elements in the document that satisfies the condition should also be present in the array I'm comparing with but that array can have additional elements.
In this case document with ArrayField satisfies condition
'ArrayField' =>
array (
'KeyA' => 'ValueA',
'KeyB' => 'ValueB',
'KeyC' => 'ValueC'
)
'ComparedArray' =>
array (
'KeyA' => 'ValueA',
'KeyB' => 'ValueB',
'KeyC' => 'ValueC',
'KeyD' => 'ValueD',
)
In this case document does not satisfy the condition
'ArrayField' =>
array (
'KeyA' => 'ValueA',
'KeyB' => 'ValueB',
'KeyC' => 'ValueC'
)
'ComparedArray' =>
array (
'KeyA' => 'ValueA',
'KeyB' => 'ValueB',
)
I'm really stuck and can't find a way how to write this query. Any suggestions?
The documents we want can be obtained by:
Checking for documents where the arrayField does not have an element,
that is not present in the comparedArray.
use $elemMatch and $nin operators to find at least one element that is present in arrayField but not in comparedArray.
Apply a negative condition using $not operrator, to the above to get the documents that we want.
Code:
db.collection.find({"arrayField":
{$not:
{$elemMatch:
{$nin:comparedArray}}}})
PHP:
$notIn= array('$nin'=>comparedArray);
$eleMatch= array('$elemMatch' => $notIn);
$not = array('$not' => $eleMatch);
$collection -> find(array("arrayField" => $not);