kind-kite-734
05/20/2022, 4:49 AMkind-kite-734
05/20/2022, 4:57 AMswift-arm-37963
05/20/2022, 4:58 AMimportant-winter-64905
05/20/2022, 11:24 AMinput
form element that I would like to include more info in the htmx request but I'm not sure how to do it.
currently:
<input hx-target="#abc_this" hx-swap="outerHTML swap:0s"
class="form-control form-control-sm" type="number"
name="nameabc" value="0.00" step="0.5"
hx-post="/projects/update" hx-trigger="change delay:0.1s">
I'd like to add more attributes to the htmx request than are here: https://www.w3schools.com/html/html_form_attributes.asp
Does anyone know how to do this?important-winter-64905
05/20/2022, 11:29 AMhx-vals
here --> https://htmx.org/attributes/hx-vals/
====================
fun when just asking makes you rethink your google search and you come up with magic
https://htmx.org/attributes/hx-include/
example
<div>
<button hx-post="/register" hx-include="[name='email']">
Register!
</button>
Enter email: <input name="email" type="email"/>
</div>
bland-coat-6833
05/20/2022, 12:29 PMdiv
with a form
would it not work without the hx-include
?important-winter-64905
05/20/2022, 1:46 PMbland-coat-6833
05/20/2022, 1:47 PMcalm-queen-64495
05/21/2022, 7:18 AMrefined-waiter-90422
05/23/2022, 11:49 PMrefined-waiter-90422
05/23/2022, 11:49 PMrefined-waiter-90422
05/23/2022, 11:50 PMmysterious-toddler-20573
05/24/2022, 3:33 AMhigh-toothbrush-44006
05/24/2022, 8:13 AMhigh-toothbrush-44006
05/24/2022, 8:14 AMrefined-waiter-90422
05/24/2022, 8:26 AMhigh-toothbrush-44006
05/24/2022, 2:52 PMcalm-queen-64495
05/25/2022, 1:53 AMimportant-winter-64905
05/25/2022, 2:11 AMimportant-winter-64905
05/26/2022, 12:53 PMhx-vals
. At least I was able to get this attribute to work without any issues where hx-include
had some issues with CSS selectors (or something like that).silly-bear-76516
06/06/2022, 3:45 PMhx-select
& hx-trigger=blur
to only replace current input element with the validation that is sent from the server.
How the view looks like:
python
class SignUpView(FormView):
form_class: forms.Form = SignupForm
template_name: str = 'form-demo.html'
success_url = reverse_lazy('form-demo')
def form_valid(self, form):
if self.request.htmx:
return self.render_to_response(self.get_context_data(form=form))
return super().form_valid(form)
How the template looks like:
html
<form method="post">
{% csrf_token %}
<div
id="name_field" {% if form.name.errors %}class="error"{% endif %}
hx-select="#name_field"
hx-post="{% url 'form-demo' %}"
hx-trigger="blur from:find input"
hx-target="#name_field">
{{form.name.label_tag}}
{{form.name}}
{{ form.name.errors }}
</div>
<div
id="email_field"
{% if form.email.errors %}class="error"{% endif %}
hx-select="#email_field"
hx-post="{% url 'form-demo' %}"
hx-trigger="blur from:find input"
hx-target="#email_field">
{{form.email.label_tag}}
{{form.email}}
{{ form.email.errors }}
</div>
<div id="repeat_email_field"
{% if form.repeat_email.errors %}class="error"{% endif %}
hx-select="#repeat_email_field"
hx-post="{% url 'form-demo' %}"
hx-trigger="blur from:find input"
hx-target="#repeat_email_field">
{{form.repeat_email.label_tag}}
{{form.repeat_email}}
{{ form.repeat_email.errors }}
</div>
<input type="submit" value="Submit">
</form>
silly-bear-76516
06/06/2022, 3:47 PMhundreds-camera-24900
06/06/2022, 6:28 PMhundreds-camera-24900
06/06/2022, 6:28 PMsilly-bear-76516
06/06/2022, 6:45 PMdiv
+ some hx-
directives. The backend is still pretty agnostic and I'm just accounting for the case where the form is valid to render the template instead of redirecting or making any change in the database. The submit action is still expected from the user and follows the normal FormView
flow...hundreds-camera-24900
06/06/2022, 7:18 PMhundreds-camera-24900
06/06/2022, 7:18 PMif self.request.htmx
could apply for submitting the form through htmx or a boosted thinghundreds-camera-24900
06/06/2022, 7:19 PMhundreds-camera-24900
06/06/2022, 7:53 PMhundreds-camera-24900
06/06/2022, 7:53 PMhx-select={{form.field.id}}