Symfony constraint LessThan Date - php

I use Symfony v5.2
I've created this constraint :
$constraint = new Assert\Collection([
'firstname' => [
new Assert\NotNull(),
new Assert\NotBlank()
],
'lastname' => [
new Assert\NotNull(),
new Assert\NotBlank()
],
'birthdate' => [
new Assert\NotNull(),
new Assert\NotBlank(),
new Assert\Date(),
new Assert\LessThan('today')
]
]);
Then I send this JSON with postman :
{
"firstname": "john",
"lastname": "doe",
"birthdate": "2030-01-01"
}
But the assert is not triggered. It seems that the lessThan is only on DateTime, but not sure.
What I've done wrong ? Do i need to make my own check for a Date ?

In your case, 2 strings are compared by the LessThan validator
You should convert the request value of birthdate to an object of DateTime before validation.

Related

Symfony Date Constraint does not allow missing field even though the field is not required

I am instantiating a constraint collection here to validate my request body for an API I am building. My idea is to validate the birthdate param only for its format, I don't need it to be required. The issue I am having is that, when I do not pass the birthdate in the request body, It throws a missing field error. Basically it treats it as a required field.
I don't know why.
$constraint = new Collection([
'fields' => [
'birthdate' => [
new Date(message: 'Please use YYYY-MM-DD format!'),
],
],
'allowMissingFields' => false,
'allowExtraFields' => true,
]);
It didn't cross my mind at first, but following #jean-max comment I figured that you should make a field optional in this case because the default config is required. So yeah this is the answer:
$constraint = new Collection([
'fields' => [
'birthdate' => [
new Optional([
new Date(message: 'Please use YYYY-MM-DD format!'),
]),
],
],
'allowMissingFields' => false,
'allowExtraFields' => true,
]);

MongoDB Alphabetically Sorting

In my MongoDB database one name field is available, in this name field data is stored in the format Aaa, Bbb, Ccc, Ddd. The first letter of the word is uppercase and remaining letter is lowercase. When I apply MongoDB sort query on this data. The sorting is not working properly
My code is like below one:-
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$pipeline = [
[ '$match' => ['listingStatus' => 'Active'] ],
[ '$sort' => ['listingParticipants.firstName' => -1]],
[ '$group' => ['_id' => '$listingParticipants.email'] ],
[ '$limit' => 10],
[ '$skip' => 0],
];
$aggregate = new \MongoDB\Driver\Command([
'aggregate' => 'test_collection',
'pipeline' => $pipeline,
'cursor' => new stdClass
]);
$cursor = $manager->executeCommand('test_database', $aggregate);
I seen multiple solution on google, they use duplicate field with lower case and apply filter on that newly created field.
But in my project I don't create new field so please help me to solve this issue.

How can i set a dynamic date for a JSON value?

Im trying to set a POST request to a file in my website with some data in JSON. There is a value response that needs to be a dynamic date so when the request is made the current date is saved in a custom field. How can i set up the code under "value" so the date is generated?
Have tried things like "variable":{ new Date(), } but can't make it work since always ends displaying the code and not a date, or the code is not validated if i add [ or { , etc.
{
"version": "v2",
"content": {
"messages": [],
"actions": [{
"action": "set_field_value",
"field_name": "bday_reg_date",
"value": "2019-06-22"
}, {
"action": "set_field_value",
"field_name": "bday_exp_date",
"value": "2019-06-29"
}
That's the code im basing mine. Everytime i access the file (apparently a .php file), a new date is generated on "value", that date is send and to a custom field called reg_date on another platform. What should be the correct way to get that dynamic value?
Thanks.
If your are trying to create a new json object in PHP you have to create an array first then use the json_encode($arr) function to convert it to json.
https://www.php.net/manual/en/function.json-encode.php
IE:
<?php
$dateNow = new DateTime('now');
$dateNextWeek = new DateTime('now');
$dateNextWeek->modify('+1 week');
$arr = [
'version' => 'v2',
'content' => [
'messages' => [],
'actions' => [
[
'action' => 'set_field_value',
'field_name' => 'bday_reg_date',
'value' => $dateNow->format('d-m-Y'),
],
[
'action' => 'set_field_value',
'field_name' => 'bday_reg_date',
'value' => $dateNextWeek->format('d-m-Y'),
],
]
]
];
return json_encode($arr);

How to add additional (multiple) values to session without overwriting the existing array?

When I try to add additional data to an session array it overwrites the old ones. Is there a way to add multiple values to session array without overwriting the old one or have I to use push one by one?
Here for example:
session()->put([
'parent'=>[
'name'=>'jhon',
'surname'=>'doe',
]
]);
Now with session()->all() I get:
[
"parent" => [
"name" => "jhon",
"surname" => "doe",
],
]
When I want to add additional values with put for example:
session()->put([
'parent'=>[
'gender'=>'male',
'phone'=>'000000',
]
]);
No I get this with session()->all():
[
"parent" => [
"gender" => "male",
"phone" => "000000",
],
]
But I want:
[
"parent" => [
"name" => "jhon",
"surname" => "doe",
"gender" => "male",
"phone" => "000000",
],
]
So how can I add additional (multiple) data to an existing session array without touching the old ones?
You can read data first, modify it and save it again. Just an example:
$data = session('parent');
$data['gender'] = 'male';
$data['phone'] = '000000';
session(['parent' => $data]);
You can use session()->push() method as given here
For e.g.
1) Put the data first time using session()->put()
session()->put([
'parent'=>[
'name'=>'jhon',
'surname'=>'doe',
]
]);
2) Now Push the data you want to add using session()->push()
session()->push('parent',[
'gender'=>'male',
'phone'=>'000000',
]);
Thank to #AlexeyMezenin and #GautamPatadiya answers I found this way which looks very convenient to me. Please correct if this seems wrong to you or there is a better way you konw.
session()->put([
'parent'=>[
'name'=>'jhon',
'surname'=>'doe',
]
]);
Then just add itself to the new array like:
session()->put([
'parent'=>[
'gender'=>'male',
'phone'=>'000000',
] + session('parent');
]);

Random data container using magic methods __get and __set

Is it possible to create a PHP class that can hold whatever type of data you throw at it, even recursively, using magic methods?
I saw this: PHP - Indirect modification of overloaded property
but it doesn't handle recursive data:
class ActiveRecord extends Creator {
}
$a = new ActiveRecord();
$a->_id = "123456789";
$a->persona_info = [
"name" => "Bob",
"surnames" => ["First", "Second", "Third"]
];
$a->history = [
"logins" => [
[
"date" => "1999",
"ip" => "1.2.3.4"
],
[
"date" => "1129",
"ip" => "1.2.3.4"
]
],
"purchases" => [
[
"date" => "1819",
"amount" => "1884"
],
[
"date" => "1459",
"amount" => "14"
]
]
];
var_dump($a->history->logins);
That gives me:
PHP Notice: Trying to get property of non-object in /tmp/x.php on line 90
PHP Stack trace:
PHP 1. {main}() /tmp/x.php:0
NULL
Trying to investigate further, I see that $a->history is a plain php array instead of a Value object (or even a Creator object.
The problem is $a->history is an array and not a object. It should be var_dump($a->history['logins']);

Categories