I’m prepping to use flatpickr, a date picking library which attaches to a specific input field. I’m curious as to the best approach to integrate this with Liveview.
I’m using alpine.js to reveal the input field and to create the flatpickr instance on my input field. However, when Liveview handles the validation on change, it seems that flatpickr becomes “detached” from the target - the instance is still in memory, but cannot be opened. In the absence of a way to transfer the existing instance to the updated DOM element, I am assuming my best bet is to track and destroy() existing instance(s) and reinitialize via a client hook called on updated.
This might be exactly the right way to do this, but any other suggestions? My JS knowledge has atrophied a bit and I’m new to Liveview, so I’m just looking for validation that I’m not overlooking something.
I’m just curious as I’m about to integrate a datepicker into my application. Is this an issue with flatpickr itself. Have you tried other datepickers and do you get the same issues?
That would probably apply to any datepicking library.
I re-read the docs and missed the fact that one could attach phx-update="ignore" to disable patching on specific DOM nodes. I added this to my input field’s parent and wallah! This obviates the need to manage specific instances.
Of course, if anyone has any better ideas, let me know!
That said, I’m surprised that Liveview patches the entire form when validating. I would have thought Liveview would focus on what was changed, not the entire form. I’m guessing that since Liveview patches elements based on changed assigns, that the round trip of the form_data is the change that causes the DOM patch… ?? I’m hoping I can find some time to look into that more and maybe open a github issue to gather more info.
Great to know about the phx-update=“ignore”. Also I think you are right about the round trip for form data as the entire form is passed back and forth even when validating a single field.
I’m going to be a bit cheeky and ask if you are able to provide your implementation of flatpickr with live-view?
I’m not sure if this is a shining example of how to do things - as mentioned I still wonder if there are easier ways. Here is a simplified example of where I am ending up - but note that I’m still in the middle of building a multi-step form using Alpine.
My man do you know how much heartburn I suffered today trying to fix this god forsaken thing and your solution pulled me from the depths of hades out of the clutches of Mephistopheles himself?
I wound up going with a very similar solution in a subsequent project, except in my case, I needed to communicate validation errors in my form. I wound up wrapping my phx-update="ignore" with a parent node <dix phx-feedback-for="target" and add some custom CSS to help render validation errors.
Would you mind showing an example of your error handling on the datepicker field? I’m doing something similar and I’m sort of new the way Elixir handles forms.
I’m having issues integrating flatpickr using LiveView 1.0. The new way of keeping track of unused inputs doesn’t seem to play nice with how flatpickr is initialized (at least not with the configuration permutations I’ve tried).
The params sent to the LV event handler (using phx-change) don’t contain the _unused_xxx parameter for the date input even if it’s not being touched. This makes the date input immediately seem used. In cases where the date input is validated as required for example, this makes the input show up with its error markup and validation error, even though another input is being edited.
Did anyone notice the same? I’ve tried the wrap option of flatpickr, but also the method of placing the altInput inside phx-ignore element. Without any luck. My hypothesis is that flatpickr “touches” the original input, which confuses the LiveView js runtime that keeps track of the used/unused inputs. But I’m unaware of the internals of this process.