Chapters
41 Chapters
|
4:45:40
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
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!
13.
The Edit Form
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.1
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.1.6
"symfony/cache": "^3.3|^4.0", // v4.1.6
"symfony/console": "^4.0", // v4.1.6
"symfony/flex": "^1.0", // v1.21.6
"symfony/form": "^4.0", // v4.1.6
"symfony/framework-bundle": "^4.0", // v4.1.6
"symfony/property-access": "^3.3|^4.0", // v4.1.6
"symfony/property-info": "^3.3|^4.0", // v4.1.6
"symfony/security-bundle": "^4.0", // v4.1.6
"symfony/serializer": "^3.3|^4.0", // v4.1.6
"symfony/twig-bundle": "^4.0", // v4.1.6
"symfony/validator": "^4.0", // v4.1.6
"symfony/web-server-bundle": "^4.0", // v4.1.6
"symfony/yaml": "^4.0", // v4.1.6
"twig/extensions": "^1.5" // v1.5.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.1.6
"symfony/dotenv": "^4.0", // v4.1.6
"symfony/maker-bundle": "^1.0", // v1.8.0
"symfony/monolog-bundle": "^3.0", // v3.3.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.1.6
"symfony/stopwatch": "^3.3|^4.0", // v4.1.6
"symfony/var-dumper": "^3.3|^4.0", // v4.1.6
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.1.6
}
}
30 Comments
This may be a little pedantic, but: I don't think your controller's
editmethod needs to say$em->persist($article), does it? Doctrine already knows we want to persist, yes?Hey @David!
You’re 100% correct. I used to do this… but these days I typically just use flush() (but persist was never needed, except for new objects).
Cheers!
Is it also possible to have the edit-form appear as a popup over the article listing?
Hey RobinBastiaan
Yes, it's possible. You only need to make an AJAX request to your backend to fetch the form's data (as HTML or JSON if you want to style up the form on your frontend). Then, just create such endpoint for fetching the form's data.
Cheers!
Why would you include the _form.html.twig but on some other parts you create a html.twig files with 'extends base.html.twig' and parent(). Why wouldnt you just include everything like _form.html.twig?
Hey @Farry7
It depends on purpose. If there is a possibility that you will re-use your form in other place, than using include
_form.html.twigyou can include it everywhere, however if you have full template which extends your base, than you can use it only as main template of action. BTW simple templates without base can be easily updated via ajax actions!Cheers!
Too many arguments to "doctrine:query:sql" command, expected arguments "sql".
Hey @Farry7!
Hmmm. Make sure you have the query surrounded by quotes - otherwise it will look like individual arguments to the command. If single quotes are working for some reason, try double-quotes. That "*" character in the query can sometimes confuse terminal systems (as it can be a special character).
Let me know if that helps!
Cheers!
Hello,
I'm trying to create an edit form starting from the edit form tutorial, I added the edit function on the controller, but when I try to print the form it gives me this error,
<blockquote>Unable to guess how to get a Doctrine instance from the request information for parameter "invoice"
</blockquote>
below my configuration:
namespace App\Repository;
use Doctrine\ORM\EntityRepository;
class InvoiceRepository extends EntityRepository
{
}
/**
/
class Invoice
{
/*
InvoiceController
Hey Gianluca,
This sounds like a problem that does not relate to the form. Could you double check that $invoice is not null and an instance of \App\Entity\Invoice in your edit() controller? You can use "dd($invoice)" or "dump($invoice); die;" in the begin of the edit(). Do you see the same error? Btw, does your Invoice entity has "invoiceNumber" property?
Cheers!
Thanks Victor,
I'm resoved like so:
Hey Gianluca,
Thanks for sharing your solution with others! Yeah, you can always query for the object manually, especially in some complex cases - that's not a big deal. Glad you solved it yourself!
Cheers!
Hello, I'm getting a App\Entity\Article object not found by the @ParamConverter annotation. I followed the video without much changed to the code. How should I solve this?
Hey Joel Bushiri
I believe the id of the article you are trying to access does not exist anymore. Could you double check that such id exists on your database?
Cheers!
Hello Ryan,
sorry to disturb but I see something strange with validation in my edit form.
In my entity I have a field "title" with @Assert\NotBlank() and validation works pretty well when I create a new record.
When I edit an existing record, delete the content of "title" field and send the form, instead of a validation warning I get a 500 error with message "Expected argument of type "string", "NULL" given at property path "title".
Thank you for your attention.
Hey Carlo,
Even if you required a value for this field in the DB i.e. the field could not be NULL, you still need to allow null in your model (entity) in case you want to use Symfony Forms, that's kind of limitation. So, allow null in your strict types for this title field, I suppose you need to do it in setter/getter:
And then it should work. As an alternative solution - you can use a separate model (like DTO) for your form where you will allow null for this field and then map data to your entity in case you don't want to allow null in your entity.
I hope this helps!
Cheers!
Cancelled my question :)
Hello,
I make a similar project based on it, it says it cannot autowire Entities, in the service.yaml, indeed they are excluded, googling I see some parts saying it's normal, and they advise to create a manager class (I don't see what it is, they mean repository?), is it good practice to exclude Entities?
One more question : How does the id in the url translates into the Article object?
Thanks for posting and thanks Benoit L. for the reply! after reading the docs and installing the SensioFrameworkExtraBundle the magic kicked in!
I worked around it with the EntityManager but i wasn't satisfied without getting the DI working!
Hey Benoit L.
Good questions :)
Entities should be excluded from being auto-wired because they are just a data model, so you should not inject any services into their constructor
> How does the id in the url translates into the Article object?
There is a bit of magic that Symfony performs when you type-hint a controller's action with an entity class, it's called "Param converter".
If you want to know more, you can watch this episode where Ryan explains it: https://symfonycasts.com/sc...
Or, you can read the docs: https://symfony.com/doc/cur...
Cheers!
All edit and new forms I see one submit button. How to implement next to the Submitbutton a Cancel button? Is that easy to do? Especially for editing forms it's great.
Hey Ginius
That's super simply, specially if you follow Symfony Form Best Practices. The "BP" is to render your buttons manually, so you can reuse your FormTypes easily. So, what you have to do is just to add some extra HTML for adding a cancel/back button
Cheers!
I understand what you meaning. My code is as follow:
<br /><button id="form_save" name="form[save]" type="submit" class="btn btn-success btn-sm">Save</button><br /><button id="form_close" name="form[close]" type="button" class="btn btn-sm" onclick="document.location.href='/admin/klanten/lijst';">Close</button><br />It need going to be back to mij main list (admin/klanten/lijst the route in the controller is 'admin_company_list' How dow I jump to that part in twig/symfony?
btw, I like to use the naming from the routing. Not a hard coded url in href
You only have to render a route using twig. Probably you may enjoy watching our Twig tutorial: https://symfonycasts.com/screencast/twig
Anyway here is how:
I had to add a tick to the anchor tag because it was been rendered as HTML
Thanks. Made a little change, above was not working.
Just toke the a-tag (removed the button).
Now it is working. Seems a-tag within button not working. Again thanks for you help and pointing me to the right direction
Wow, I didn't know you can't add an anchor tag inside a button, that's new to me :)
Cheers man!
what if I ve got a photo attribute in the database which i dont want to update in the same function it shows me an error "The file could not be found"
Hey Dave!
We're currently talking about file uploads in our uploads tutorial - and I think what you're talking about is covered here: https://symfonycasts.com/sc...
However, the error you're seeing is a bit odd - it comes from the File validation constraint. Normally, if you choose not to upload a file and you submit, the File constraint is smart enough to see that you didn't upload anything, and it does nothing. However, in your case, it *does* seem to be validating still - except that the uploaded file is missing. My guess is that there's something "quirky" in your setup. Can you post your form and controller code? And when exactly dose this error occur?
Cheers!
the solution I found was to check if the user has uploaded any photo in the FileType if yes it updates it if not it sets the actual photo
Hey dave!
Yep, that works fine! In the file uploads tutorial, we use an "unmapped" field, which is kinda nice, because the file upload doesn't override a property on your entity. But, both totally work!
Cheers!
"Houston: no signs of life"
Start the conversation!