Chapters
46 Chapters
|
4:55:39
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
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!
33.
Opening a Modal
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 perfectly with Stimulus 3!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "1.11.99.1", // 1.11.99.1
"doctrine/annotations": "^1.0", // 1.11.1
"doctrine/doctrine-bundle": "^2.2", // 2.2.3
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.2
"doctrine/orm": "^2.8", // 2.8.1
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"sensio/framework-extra-bundle": "^5.6", // v5.6.1
"symfony/asset": "5.2.*", // v5.2.3
"symfony/console": "5.2.*", // v5.2.3
"symfony/dotenv": "5.2.*", // v5.2.3
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/form": "5.2.*", // v5.2.3
"symfony/framework-bundle": "5.2.*", // v5.2.3
"symfony/property-access": "5.2.*", // v5.2.3
"symfony/property-info": "5.2.*", // v5.2.3
"symfony/proxy-manager-bridge": "5.2.*", // v5.2.3
"symfony/security-bundle": "5.2.*", // v5.2.3
"symfony/serializer": "5.2.*", // v5.2.3
"symfony/twig-bundle": "5.2.*", // v5.2.3
"symfony/ux-chartjs": "^1.1", // v1.2.0
"symfony/validator": "5.2.*", // v5.2.3
"symfony/webpack-encore-bundle": "^1.9", // v1.11.1
"symfony/yaml": "5.2.*", // v5.2.3
"twig/extra-bundle": "^2.12|^3.0", // v3.2.1
"twig/intl-extra": "^3.2", // v3.2.1
"twig/twig": "^2.12|^3.0" // v3.2.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.0
"symfony/debug-bundle": "^5.2", // v5.2.3
"symfony/maker-bundle": "^1.27", // v1.30.0
"symfony/monolog-bundle": "^3.0", // v3.6.0
"symfony/stopwatch": "^5.2", // v5.2.3
"symfony/var-dumper": "^5.2", // v5.2.3
"symfony/web-profiler-bundle": "^5.2" // v5.2.3
}
}
What JavaScript libraries does this tutorial use?
// package.json
{
"devDependencies": {
"@babel/preset-react": "^7.0.0", // 7.12.13
"@popperjs/core": "^2.9.1", // 2.9.1
"@symfony/stimulus-bridge": "^2.0.0", // 2.1.0
"@symfony/ux-chartjs": "file:vendor/symfony/ux-chartjs/Resources/assets", // 1.1.0
"@symfony/webpack-encore": "^1.0.0", // 1.0.4
"bootstrap": "^5.0.0-beta2", // 5.0.0-beta2
"core-js": "^3.0.0", // 3.8.3
"jquery": "^3.6.0", // 3.6.0
"react": "^17.0.1", // 17.0.1
"react-dom": "^17.0.1", // 17.0.1
"regenerator-runtime": "^0.13.2", // 0.13.7
"stimulus": "^2.0.0", // 2.0.0
"stimulus-autocomplete": "^2.0.1-phylor-6095f2a9", // 2.0.1-phylor-6095f2a9
"stimulus-use": "^0.24.0-1", // 0.24.0-1
"sweetalert2": "^10.13.0", // 10.14.0
"webpack-bundle-analyzer": "^4.4.0", // 4.4.0
"webpack-notifier": "^1.6.0" // 1.13.0
}
}
22 Comments
The modal import has some very quirky issue where it kills dropdowns elsewhere on the page. See https://stackoverflow.com/q....
The fix is to change the import to
import Modal from 'bootstrap/js/dist/modal';
He Tac,
how did you get it to work? When i change import {Modal} from 'bootstrap'; to import {Modal} from 'bootstrap/js/dist/modal'; i get i error:
bootstrap_js_dist_modal__WEBPACK_IMPORTED_MODULE_15__.Modal is not a constructor
i have this in my controller:
let modal = new Modal(modalObject);
Hey Scott S.!
I still don't understand why the different import behaves differently, but I believe if you do this, you need to import
Modalinstead of{Modal}:Cheers!
Hey Tac,
Thank you for sharing a possible fix with others!
Cheers!
Hi everyone. I'm not sure it's the right place for my comment, but I saw you talked about select2 there so... I'm working on using stimulus build on select2 box. I would like to execute some code on change, but I not figured out the right way now.
In my base.html.twig I have
Here's my template :
and finally in my stimulus controller named select2_controller.js:
Could you help me figured out what's missing ? Thank you !
By the way, I LOVE your work, very helpful in a beautiful funny way !
Hey @dioupy!
Sorry for the slow reply! Busy time of year!
I think (unless it's someone else with the exact same question) that we chatted on Slack already a bit. Generally-speaking, though I don't know select2 specifically well, your code looks ok to me. Btw, where/how is Select 2 itself initialized? Is that in your Stimulus controller but you didn't show it here?
Anyway, if you're still having issues, let me know what is and isn't working and we'll see what we can figure out :).
Cheers!
Hi all, but am I the only one forced to use the stimulus controllers in this way to make them work ?
...note the first line, the name of the library from which it imports and the comment (stimulusFetch: 'lazy') without which the controller seems dead !!!
Hey @pasquale_pellicani
Good catch. The
importstatement has changed in a recent version fromstimulusto what you have@hotwired/stimulus. So that's fineAbout the comment line, that's a trick to mark a controller as lazy loading. In other words, that controller will be only loaded if something in your page uses it
Cheers!
thanks @MolloKhan for reply, actually to make the example in this guide work, I modified the code like this:
...but it seems that something anticipates the stimulus controller, because I receive this error which seems to be due to the fact that the element does not yet exist at the time of the click event
error code:
thanks to all
P.S.
the modal opens correctly, but I want to resolve the error :)
[solved]
remove from button data-bs-toggle="modal"
button become :
Hey @pasquale_pellicani
Sorry for my slow reply, a long weekend got in the middle :)
Oh ok, so the problem was a Bootstrap modal thing and not actually a Stimulus error. I'm glad to know that you found a solution.
Cheers!
Is there a more universal way to handle modals? This tutorial is perfect but can it apply to all the forms that I will have to manage in a project or will I have to make as many stimulus controllers as many modals I will have to manage? I found this on the net https://github.com/Sideclick/BootstrapModalBundle too bad it's not compatible with the bs5.
Thanks ;)
Hi @pasquale_pellicani,
All you need is 1 universal stimulus controller, which can be re-used everywhere and apply it to all forms you need, of course you will need to add some more twig code to use it.
Cheers!
Hi!
For me everything was working great until I opened a form on a modal with select2 or CKEeditor fields... they fail to load with no error... so I have a form but no select2, CKEditor, datatable functionality....
does anyone know how to make it work?
I got the select2 elements to work with the following code from after the
this.modalBodyTarget.innerHTML = await response.text(); line:
but I don't know if it is the right way and I don't know how to run CKEditor either (using FOSCKEditor)
Hey @ikerib!
Ok! Let's see if we can fix this :).
First, about the code above. It's more or less valid, but we can improve :). The problem with code like this is that it will only affect elements that are ON the page the moment you run this. So, for example, if you moved this into
assets/app.js(as an example), it would correctly transform all of the elements... that CURRENTLY exist on the page. But if any new elements are added later, nothing would happen with those. That's why you added this after thethis.modalBodyTarget.innerHTML = await response.text();line: you're basically re-initializing your JavaScript now that you have new content on the page. This works, but it's a bummer to need to do this whenever new content is loaded onto the page via AJAX.Fortunately, this is where Stimulus shines. I'll use the
.nireselect2as an example:A) Instead (or in addition to) the
nireselect2class, you should render a new stimulus controller onto the select element. I'll assume you're using the form component:B) Create a new
assets/controllers/select2-controller.jsfile:That's it! The key is that, as soon as the
data-controller="select2"pops onto the page - no matter HOW it's loaded onto the page - theconnect()method in your controller will trigger and it will initializeselect2on that element (this.element).For the dater picker, it would be a
date-pickercontroller that does the same thing.For FOSCKEditor, I'm not really sure, as IT, I believe tries to initialize CKEditor on your behalf. Personally, I would skip that bundle and create a small Stimulus controller (like above) to initialize CKEditor. Or use something like https://github.com/basecamp/trix - where all you need to do is render a
<trix-editor input="x"></trix-editor>element and it "just works".Let me know if this helps!
Cheers!
Ryan,
I've just been reading through the comments and saw posts like these from you.
I just wanted to say thank you for taking the time in providing help in the comments.
I'm sure my sentiment is shared with others and that you are greatly appreciated by the silent majority.
Thanks again man.
❤️❤️❤️
Wow! Ok! I undestand what you say, thanks a lot! I will try all tomorrow ;)
Everything working!! Thanks!!
Hey Guys;
We don't have a tutorial today?
Sorry It O.! That's totally my fault - I got behind yesterday! This tutorial especially (because I've been improving Stimulus with Symfony while making the tutorial!) has been a particularly hard one to get consistently out. But, I *did* just record the 2nd to last video... and so I'll be getting the audio recorded for those ASAP. The last video is, of course, waiting on a pull request to get merged (I hope!) that I just created today - https://github.com/afcapel/... - it's open source meets tutorial building :).
Anyways, more to come soon - with any luck, the last few will come faster than one every day.
Cheers!
great!!! thanks for answering
"Houston: no signs of life"
Start the conversation!