How to validate an array inside an array payload in laravel - php

How to validate a request like for this example i want to create a custom validation for qualities based on a group type. I know how to create a custom validation for laravel but for the example below i want to create a validation for quality type based on its group type.
The payload below it just for demonstration.
$payload = [
'groups' => [
[
'type' => 'human',
'qualities' => [
[
'type' => 'hair',
'value' => 'blue'
],
[
'type' => 'height',
'value' => '188cm'
],
]
],
[
'type' => 'cat',
'qualities' => [
[
'type' => 'hair',
'value' => 'yellow'
]
]
]
]
];

You can use the wildcard, for example:
$request->validate([
'payload.*' => 'required|array',
'payload.*.type' => 'required',
'payload.*.qualities' => 'required|array',
'payload.*.qualities.*' => 'required'
]);

Related

How to add a custom field to orders in Shopware 6?

By default you can add custom fields to several entities, however I don’t see the order entity in the list of available entities.
Is it possible to add such a field for order so user can fill it in the checkout process, right before sending the order?
And is it possible to add a field for the order and for each order item individually?
Here is one example on how to add to add custom fields to order entity:
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->upsert([
[
'name' => self::FIELD_NAME,
// 'global' => true,
'config' => [
'label' => [
'de-DE' => 'Name',
'en-GB' => 'Name'
]
],
'customFields' => [
[
'name' => 'name',
'type' => CustomFieldTypes::DATETIME,
'config' => [
'type' => 'date',
'dateType' => 'date',
'label' => [
'de-DE' => 'Date',
'en-GB' => 'Date'
]
]
],
[
'name' => 'name',
'label' => "Time",
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'name',
'en-GB' => 'name'
]
]
],
[
'name' => 'name',
'label' => "name",
'type' => CustomFieldTypes::INT,
'config' => [
'label' => [
'de-DE' => 'name',
'en-GB' => 'name'
]
]
]
],
'relations' => [[
'entityName' => 'order'
]],
]
], $context);

Implement suggest function of elasticsearch-php client in my API

I am trying to implement the suggest function of elasticsearch-php client in my API to suggest people some already existing problems.
I have made index for my problems
'index' => 'newproblemindex',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'newproblems' => [ // type of index
'_source' => [
'enabled' => true
],
'properties' => [
'title' => [
'type' => 'text',
'analyzer' => 'standard'
],
'description' => [
'type' => 'text',
'analyzer' => 'standard'
], 'suggest' => [
'type' => 'completion'
]
]
]
]
]
But I am unable to find which param fields to use to implement suggest function
'index' => 'newproblemindex',
'body' => [
'try' => [
'text' => $request->search_key,
'completion' => [ 'text' => 'suggest' ]
]
],
I am using laravel and taking search_key as request param but I am getting "invalid_type_name_exception" and when I tried to give the type name, it is again giving me some error.
"suggest" => [
"song-suggest" => [
"prefix" => $request->search_key,
"completion" => ["field" => "suggest"]
]
]
I am getting error "suggest is not a valid param ". Please help
and Thanks in advance.

Combine mongoDB $lookup with $project

Right now I'm using the $project aggregation for filtering out unnecessary fields. Also I'm using the $lookup aggregation to link two collections togethe and I know how to use both of them in the main collection.
Now my question is; how can I put this $project aggregation inside of a lookup?
What I have now is looks like this:
[
'$lookup' => [
'from' => Media::collectionName(),
'localField' => '_id',
'foreignField' => 'project_id',
'as' => 'mediaList'
]
],
[
'$project' => [
'title' => 1,
'owner_id' => 1,
'owner_name' => 1,
'created_at' => 1,
'updated_at' => 1,
'status' => 1,
'discount' => 1,
'company' => 1,
'media' => [
'$filter' => [
'input' => '$mediaList',
'as' => 'media',
'cond' => $mediaFilter
]
]
]
],
So I can filtering out the unnecessary fields in the main collection. How can I do this in the sub-collection?
If I understood your question correctly, you want to apply a $filter operation on your $mediaList field.
To avoid a redundant $project stage (where you have to declare every single field you want to keep), use $addFields stage instead, like this :
[
'$lookup' => [
'from' => Media::collectionName(),
'localField' => '_id',
'foreignField' => 'project_id',
'as' => 'media' // Note that I use the same name for the field
]
],
[
'$addFields' => [
'media' => [
'$filter' => [
'input' => '$media',
'as' => 'media',
'cond' => $mediaFilter
]
]
]
],

Zend Framework 2 - Use custom validators in input filter

I want to use a custom validator with the input filter add method. I am using the latest version of the zend-validator package.
In my module.config.php I have this:
'validators' => [
'invokables' => [
'DoctrineRecordExists' => 'Utils\Validator\Doctrine\RecordExists',
'NoDoctrineRecordExists' => 'Utils\Validator\Doctrine\NoRecordExists',
],
]
And I use it here:
$this->add([
'name' => 'name',
'required' => true,
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 3,
'max' => 20,
],
],
[
'name' => 'NoDoctrineRecordExists',
'options' => [
'repository' => 'Account\Entity\OAuthUser',
'field' => 'name',
'entityManager' => $this->getEntityManager(),
],
],
],
]);
But the constructor of NoRecordExists is never called. I get a 500 Error with no Exceptions available. If I remove the validator from the input filter, it works as expected.
I also tried this in my Module.php
public function getValidatorConfig() {
return [
'invokables' => [
'DoctrineRecordExists' => 'Utils\Validator\Doctrine\RecordExists',
'NoDoctrineRecordExists' => 'Utils\Validator\Doctrine\NoRecordExists',
],
];
}
But there was no difference.

ElasticSearch Index a Document fails on existing index

I am using ES php library. Here is what i have tried...
$params = [
'index' => 'tasks',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'all' => [
'_source' => [
'enabled' => true
],
'properties' => [
'task' => [
'type' => 'string',
'analyzer' => 'standard'
],
'to' => [
'type' => 'string',
'analyzer' => 'standard'
],
'category' => [
'type' => 'integer',
'analyzer' => 'keyword'
]
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
It returns success.
Now when i try to index a document...
$params = [
'index' => 'tasks',
'type' => 'all',
'id' => 'some_id',
'body' => [ 'task' => 'some test', 'to' => 'name', 'category' => 1]
];
$response = $client->index($params);
This throws error and does not work, however it works If i try this without creating index and mapping first.
Please suggest. Thanks
It's wrong to define analyzer in a field of type 'integer'.
Trying to create this mapping through Elasticsearch-PHP gives me a bad request:
... "reason":"Mapping definition for [category] has unsupported parameters: [analyzer : keyword]"}},"status":400}
Trying to create this mapping directly via PUT to ES gives me same error
I'm using ES version 2.2.0 and Elasticsearch-PHP 2.0

Categories