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!
44.
Using the autocomplete-controller
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
Nothing happens when I type in the search input. No Ajax calls. Just nothing.
In bootstrap.js, my editor says:
Yo @Ryan-L!
Hmm. That message from your editor MAY just be a warning that you can ignore: it looks like it's trying to get some metadata about the "type" behind
stimulus-autocompleteand it's ok if it can't find anything.In your brower's console, do you see any messages about the
autocompletecontroller being "initialized" or "connecting"? That'd be the first thing I'd check: we need to make sure that Stimulus is seeing yourdata-controller="autocomplete"element AND yourautocompletecontroller and initializing the controller.Cheers!
For the version 3 of this autocomplete controller thety must repaired something as results are visible even without role="option". But they are not accessible by keyboard without that.
Hey Peter L.!
Ah, thanks for the tip! So, if I understand it, you *should* still have the role="option"... but it's not quite as catastrophic if you forget it :).
Cheers!
I am currently using a javascript based autocomplete function, based on the description from "Symfony 4 Forms: Build, Render & Conquer!" . It is used not only as a single item form, but also to select several users in a single form. Creating a SearchUserType was very helpful, including the rendering in Javascript etc. How do I create a similar FormType with the stimulus-controller?
Hey chessserver!
Excellent question! The PHP side of things would basically be the same... you would mainly just be "converting" the JavaScript from that tutorial into a Stimulus controller, which should feel very nice actually :).
Here are a few pointers:
A) Instead of adding a js-user-autocomplete class to the form element, you would add a
data-controller="autocomplete"attribute (assuming you call your controller "autocomplete": https://symfonycasts.com/screencast/symfony-forms/autocomplete-js#codeblock-d8e290f178B) Similar to the above, you would pass the URL in as a "value" to the controller. So, assuming your controller name is "autocomplete" and you create a value called
url, you would set adata-autocomplete-url-value.With (A) and (B), when your field renders, it will contain the needed attributes to activate an "autocomplete" Stimulus controller and pass the url into it as a "url" value. The last step would be to:
C) Convert the JavaScript into a Stimulus controller. It would look something like this:
That should be it :). This uses connect to initialize the autocomplete functionality. We reference the input via this.element... and we grab the URL from this.urlValue. This, like all things with Stimulus, has the advantage over the code in that tutorial that even if you loaded a form via Ajax, this behavior would correctly be initialized.
Let me know if this helps!
Cheers!
@Ryan, thanks for your answer! This works with the original setup. However, I have been a bit unspecific. I would like to replace the above autocomplete mechanism with the stimulus-autocomplete controller introduced in this lesson.
I am struggling to setup the div_layout for the new widget correctly. The base structure should be similar to the from in product/index.html.twig. But how to transfer the url and the searchTerm correctly? Is a wrapper around the presented controller required, replacing the .autocomplete in your example above? How would the whole block starting with $(this.element) look like?
I want to implement some other 3rd-party stimulus-controllers as well, so this would help me to progress. Unfortunately, my JS Knowledge is limited.
Hey chessserver!
Apologies for my slow reply! Ok, let me give you some pointers (which are hopefully helpful!) and then you can tell me what else might be unclear (I usually reply faster than this time!).
Ok, on a high level, here is how this will probably work:
A) In your form field template, you will need one element that is around everything - e.g. a div - and this will have {{ stimulus_controller() }} rendered onto it.
B) How to pass the URL? You can pass the "url" to stimulus_controller as a value (i.e. the 2nd argument to the controller). You could hardcode this - e.g.
url: path('some_route')or you could pass the URL into the template as a Twig variable. You would do this by (A) creating a custom "field option" on your new field - e.g. url - and then (B) in buildView() of your custom field, you read that option and set a custom "variable" called "url". We do something similar here: https://symfonycasts.com/screencast/symfony-forms/build-view#codeblock-8c63ee1cf8. You could read the "url" from $options and then create a new$view->vars['url'] = $options['url'];. Then, in 2nd arg of stimulus_controller, you would have{ url: url }C) With this setup, your stimulus_controller is on an element around the input, but not actually on the input. That's ok! You could set a "target" on the input - using stimulus_target - and then use that in the controller. Or, as a shortcut, because you will only have one input element inside your controller, you could just look for that. So you would have something like:
The only change was looking for the "input" element from within this.element.
I think there are still other moving pieces... but let me know if this helps :).
Cheers!
Hi Ryan!
Do you think that most of third-party controllers are low quality?
Hey Eugem!
Hmm. I think that's a case-by-case basis. The stimulus-use library (not exactly controllers) are super high quality. https://stimulus-components... also look high quality. The controller in this chapter is... works really well... but it is *not* the highest quality. It lacks tests mostly, which I would expect any high quality library to have.
So, it's like PHP libraries or bundles - it varies widely. There are definitely both types out there.
Cheers!
Hello,
I am trying to use stimulus clipboard. It works pretty good but when I try to import it "lazy" using the symfony bridge it fails with an error coming from a file called blessing.js in stimulus core - so I am missing a blessing :(
I mean..
import { Clipboard } from '@symfony/stimulus-bridge/lazy-controller-loader?lazy=true&export=Clipboard!stimulus-clipboard'
`This doesn't work
Hey elkuku
That's odd, it makes me wonder if there's some bug on Symfony UX or Stimulus itself. Did you register the Clipboard module after importing it?
app.register('clipboard', Clipboard);Does autocomplete lib support `debouce` options like in original `search-preview` ?
Hey vLoght!
That's an excellent question that I completely forgot to address :). Yes, it does debouncing by default - 300ms - but it's not configurable (this library works super well, but it's dead-simple).
Cheers!
It can help for for people living in the past ⌛️
yarn upgrade @symfony/stimulus-bridge@2.1.0 --dev
https://github.com/symfony/...
Hey palaciospoa
IIRC this course IS using stimulus-bridge version 2.1.0 so what exact issue will fix your recomendation?
Cheers!
Just to mention that people that have this course since it was launched, have to look into their package dependencies.
I have the trouble of not having the feature of laziness via query parameter. BTW it is awesome great work 👍
Great! Thanks for your tip! BTW it's always better to download new code from course page that will help to avoid such issues!
Cheers!
Hi! It's possible combine SmartWizard with Webpack? I tried adding the following command "require("smartwizard"); and initialize $('#smartwizard').smartWizard();", but on website in console I have the following error:
What I'm doing wrong?
Hey Gareth,
I personally didn't use SmartWizard jQuery plugin, but did you try to add ".autoProvidejQuery()" to your Webpack Encore config? See this page for more details:
https://symfonycasts.com/sc...
Please, don't forget to restart your webpack encore after this change! :) Does it help you?
Cheers!
Thanks for help Victor, but effect it's that same. Console return error like above and before restarted webpack encore. Even I tried this code added to webpack, but still nothing.
Datatables works in a similar way and there is no problem with reading jQuery, but I had to add code to webpack.config.js
Could you add a smartWizard to your project and try to run it? If you have no problems, there may be something wrong with my webpack settings, or some additional library is missing.
Hey Wru M.!
I don't know that library, but here's my guess (this is from looking at their source code too). It seems like the usage is supposed to be:
This is a bit of a guess... but if I'm wrong you could do some debugging. This is the part of the code that should be executing when you import the "smartwizard" module - https://github.com/techlab/jquery-smartwizard/blob/21681e8aea67376d2d3743b4a27b7ecdc49babaf/dist/js/jquery.smartWizard.js#L26-L42 - that file would live at
node_modules/smartwizard/dist/js/jquery.smartWizard.jsin your project, and you could be some console.log() there to learn more.Good luck!
"Houston: no signs of life"
Start the conversation!