Chapters
39 Chapters
|
4:45:13
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3, <8.0
Subscribe to download the code!Compatible PHP versions: ^7.1.3, <8.0
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
21.
Validation Groups
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial works great for Symfony 5 and API Platform 2.5/2.6.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3, <8.0",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^2.1", // v2.4.5
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.13.2
"doctrine/doctrine-bundle": "^1.6", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^2.0", // v2.0.0
"doctrine/orm": "^2.4.5", // v2.7.2
"nelmio/cors-bundle": "^1.5", // 1.5.6
"nesbot/carbon": "^2.17", // 2.21.3
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0", // 4.3.1
"symfony/asset": "4.3.*", // v4.3.2
"symfony/console": "4.3.*", // v4.3.2
"symfony/dotenv": "4.3.*", // v4.3.2
"symfony/expression-language": "4.3.*", // v4.3.2
"symfony/flex": "^1.1", // v1.21.6
"symfony/framework-bundle": "4.3.*", // v4.3.2
"symfony/http-client": "4.3.*", // v4.3.3
"symfony/monolog-bundle": "^3.4", // v3.4.0
"symfony/security-bundle": "4.3.*", // v4.3.2
"symfony/twig-bundle": "4.3.*", // v4.3.2
"symfony/validator": "4.3.*", // v4.3.2
"symfony/webpack-encore-bundle": "^1.6", // v1.6.2
"symfony/yaml": "4.3.*" // v4.3.2
},
"require-dev": {
"hautelook/alice-bundle": "^2.5", // 2.7.3
"symfony/browser-kit": "4.3.*", // v4.3.3
"symfony/css-selector": "4.3.*", // v4.3.3
"symfony/maker-bundle": "^1.11", // v1.12.0
"symfony/phpunit-bridge": "^4.3", // v4.3.3
"symfony/stopwatch": "4.3.*", // v4.3.2
"symfony/web-profiler-bundle": "4.3.*" // v4.3.2
}
}
22 Comments
also add return type above getPlainPassword function
after setup this things it worked perfectly for create new post. i mean POST request. but when i try for PUT operation
got this error
TypeError : App\Entity\User::getPlainPassword(): Return value must be of type string, null returnedi spend so much time solving it. but i can't. could you please help me!
Hey Covi A.!
Ah, I see! Ok, because you're using return types on your methods (good job!), you need to change getPlainPasswords() return type to
?string- i.e. a nullable string. Think about it: when you run a PUT operation, API Platform/Doctrine queries the database for your User. That User object will havenullfor itsplainPasswordproperty, since that is not a persisted property. Then, something (I'm not sure exactly what, but the stack-trace would show you), something callsgetPlainPassword()where you try to return null, but your return-type isstring. Change it to?stringand you should be good :).Cheers!
it works,
Thank you very much.
Hi, when i try to modify with put request it returns 400 status code which is the expected response but the problem is that it modifies the database even if the response status code is 400 can you please help find the problem
Hey Sardar K.!
Ah! So, this means that - SOMEWHERE in your code (or potentially 3rd party code) you have something that, during the request, is running
$entityManager->flush(). This is a weird, unfortunate thing with how Doctrine works. When you make the PUT request, you are modifying the User entity. That is no problem... unless someone calls$entityManager->flush(). If that happens, Doctrine will save ALL entities that it's aware of, which will include persisting your modified User object to the database.See if you can track down where & why the ->flush() call is coming from.
Cheers!
hi, how can we use php8 syntax for validation groups
hi, how can we use php8 syntax for validation groups , is it possible?
Hey,
If you're on Symfony 5.2 I believe it should just work. Have you give it a try?
You can read a bit more about it here: https://symfony.com/blog/ne...}
Cheers!
Where the createUserAndLogin come from ?
Hey Ben,
It comes from the parent class, see CustomApiTestCase for more info. Btw, I'd recommend you to use PhpStorm, when you will hold Command button and press on the method name - it will open the method from parent class for you.
Cheers!
This is what i thought, I didn't found in which chapter we did it.
(I prefere do it all manually than copy paste things)
I'm not getting all green after defining the groups for the NotBlank and I'm still getting password should not be blank. I've copy and pasted all relevant code from the script to make sure I'm not making any typos. According to the error it's failing on the PUT.
`1) App\Tests\Functional\UserResourceTest::testUpdateUser
Failed asserting that the Response is successful.
HTTP/1.1 400 Bad Request
Cache-Control: max-age=0, must-revalidate, private
Content-Type: application/ld+json; charset=utf-8
Date: Thu, 15 Oct 2020 23:42:43 GMT
Link: <http://example.com/api/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"
Set-Cookie: MOCKSESSID=ea6ddfddc68e01293646922b743207dd0f6c9a208b1a40be31044517388a83d9; path=/; secure; httponly; samesite=lax
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-Robots-Tag: noindex
{"@context":"\/api\/contexts\/ConstraintViolationList","@type":"ConstraintViolationList","hydra:title":"An error occurred","hydra:description":"password: This value should not be blank.","violations":[{"propertyPath":"password","message":"This value should not be blank."}]}
C:\Users\ack\PhpstormProjects\API-Platform-Training2\src\ApiPlatform\Test\BrowserKitAssertionsTrait.php:42
C:\Users\ack\PhpstormProjects\API-Platform-Training2\tests\Functional\UserResourceTest.php:32`
Hey @Aaron!
Hmmm. What happens if you *remove* the @Assert\NotBlank from the plainPassword field (and, you *do* have it on the plainPassword field, and not password, right?)? I'm curious to see if it saves correctly (meaning the field IS being set, but something is wrong with validation) or it explodes when trying to INSERT because the password field is blank.
Here are other things I would check:
A) Make sure the plainPassword is in the user:write group and has @SerializedName
B) Make sure there is a setPlainPassword() method (and double-check for any typos there).
Let me know what you find out! It sounds like probably a small missing detail somewhere. At the end of the day, this "plainPassword" field is a normal API field... and if it's blank (even though you're sending the "password" field), then something is slightly wrong. Oh, and feel free to post some code!
Cheers!
It worked removing it and then after adding it back it still worked. I can't understand how that works so I gotta assume I managed to make a typo even copying and pasting if that's even a thing which apparently it must be. Thanks!
Ha! Gremlins in your computer. I’m glad you got it working :).
Cheers!
Indeed. By the way -- it seems like there's a typo in the last command. Isn't the folder tests and not test? Somehow it seems to automagically work though.
Ah, good catch - you're totally right. Fixed! https://github.com/SymfonyC...
Thanks!
After adding the validation_groups i get following deprecation notices and i dont really understand what they are about :)
` 3x: Not setting the "method" attribute is deprecated and will not be supported anymore in API Platform 3.0, set it for the collection operation "validation_groups" of the class "App\Entity\User".
Hey Hannah R.
Looks like there will be some upcoming changes in api platform configuration. There is nothing to worry about because when version 3.0 will be released all configuration changes should be listed. The weird situation that it's showing that
validation_groupsis a collection operation, did you put it in correct place? if so try to add"method"="POST"to see if it change errorsIt should be like:
Cheers!
Hi,
This video confirms something I've been thinking on every video of this course.
It seems you are mixing 'PATCH' with 'PUT' responsibilities.
'PUT' should have REQUIRED password the same as CREATE because it's a REPLACEMENT of the object a complete and valid object to replace.
Usually `PATCH` is used for partial modification of objects such as stated "if a user want only to modify it's username"
Am I missing something? I know this kind of details are usually messed up but this course seems to be so perfect and consistent with best-practices that I needed to ask
From : https://api-platform.com/do...
PUT: Replace an element.
PATCH: Apply a partial modification to an element.
Thanks for this great course!
Hey Qcho
What you stated is correct. Put is for updating a whole resource and Patch is for a partial update where you only send the info you want to update.
You may or may not want to send the password on a PUT request, it depends on your security policies.
Cheers!
If I understant well, you mean that 'password' si a special field, but apart from 'password', I understant from your answer, what is done in this video is not really standard, thus,you should either
- Make a PATCH request insteat of the PUT request
- Inculde all fields (maybee you can exclude password) in the PUT request
"Houston: no signs of life"
Start the conversation!