Skip to content

Instantly share code, notes, and snippets.

@cyberfly
Last active December 19, 2023 09:11
Show Gist options
  • Save cyberfly/60fabe5bcc146a3aacd0f07f56e8ac27 to your computer and use it in GitHub Desktop.
Save cyberfly/60fabe5bcc146a3aacd0f07f56e8ac27 to your computer and use it in GitHub Desktop.
Add validation to hx-trigger. If validate false, hx-post is not trigger `hx-trigger="submit[!isFieldEmpty('#seed_keyword')]`
<form
id="keyword_suggestions_form"
hx-post="/api/v1/keywords"
hx-target="#keywords_container"
hx-indicator="#loader_container"
hx-on:htmx:before-request="emptyDiv('#keywords_container')"
hx-trigger="submit[!isFieldEmpty('#seed_keyword')], submitKeywordSearch from:body"
>
<button type="submit">Search</button>
</form>
window.isEmpty = function isEmpty(variable) {
variable = $.trim(variable);
return variable === undefined || variable === null || variable === "";
};
window.isFieldEmpty = function isFieldEmpty(id) {
let value = $(id).val();
return isEmpty(value);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment