ideas
ideas copied to clipboard
A barebones form JS driver
I've been working on implementing form conditions on my frontend forms (which is such an amazing feature!), and I think a barebones, library agnostic JS driver might be a useful addition to core.
I'm using blade with a set of custom blade components to render my forms. Although I do use Alpine, I don't really need any of the magic that the AlpineDriver provides as my forms are already Alpine components. All I actually need to get everything working is the array of form values, the array of field conditions, and the helper (which I can call from my existing Alpine component code).
So I've implemented the following custom JS driver:
class Barebones extends AbstractJsDriver
{
public function addToFormData($data)
{
return [
'values' => $this->getInitialFormData(),
];
}
public function addToRenderableFieldData($field, $data)
{
return [
'show_field' => $field->conditions(),
];
}
}
And this is how I use that data:
<form ... x-data="form({{ json_encode([
'values' => $form['values'],
'conditions' => $form['show_field'],
...
]) }})">
As you can see there's no Alpine in the driver, it just provides the minimum data needed to make conditions work when implementing your own logic (or another library) in your template code/JS bundle.
If you think this would be a useful addition I can PR it.
I've been working on implementing form conditions on my frontend forms (which is such an amazing feature!)
🎉
All I actually need to get everything working is the array of form values, the array of field conditions, and the helper (which I can call from my existing Alpine component code) [...] As you can see there's no Alpine in the driver, it just provides the minimum data needed to make conditions work when implementing your own logic (or another library) in your template code/JS bundle.
Super cool to see people making custom JS drivers like this already, and this is a neat example ❤️
I'm just a bit unclear how we'd document this; If the examples in the docs raise more questions than answers, I would want to be careful that we don't encourage it over the simplicity of the alpine driver, in fear of confusing people in the docs. If others want this though, we'd be open to a PR for sure!
I'm just a bit unclear how we'd document this
Yeah good point. I guess if it was called something like "Advanced Driver" that might make it clear that it's the more complicated/involved option, but even then it could still cause confusion.
Anyway, will leave it for now, see if anyone else wants it first.