Skip to content

Instantly share code, notes, and snippets.

@leolll
Created February 1, 2021 05:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leolll/b5ac7ef8710d31bcd948b48de5c1147b to your computer and use it in GitHub Desktop.
Save leolll/b5ac7ef8710d31bcd948b48de5c1147b to your computer and use it in GitHub Desktop.
Radio button validation + iodine.js
<label for="password">Plan</label>
<input name="plan" type="radio" id="pro" value="pro" x-bind:class="{'invalid':plan.errorMessage}" data-rules='["requiredChoice:plan"]' data-server-errors='[]'>
<input name="plan" type="radio" id="basic" value="basic" x-bind:class="{'invalid':plan.errorMessage}" data-rules='["requiredChoice:plan"]' data-server-errors='[]'>
<p class="error-message" x-show.transition.in="plan.errorMessage" x-text="plan.errorMessage"></p>
Iodine.addRule("requiredChoice", (value, name) => {
return document.querySelector('input[name="' + name + '"]:checked') != null;
});
Iodine.messages.requiredChoice = "Please select one of these options";
@leolll
Copy link
Author

leolll commented Feb 1, 2021

If you're following this tutorial and need to validate radio inputs, you'll want to create a custom rule in Iodine that passes an additional param. That parameter should match the radio button name, which is the same for each radio input in the group.

@noum
Copy link

noum commented Jul 15, 2021

This should work for checkboxes too right?

@leolll
Copy link
Author

leolll commented Jul 15, 2021

It's been a while since I worked on this so I'm not sure whether or not it'll work for checkboxes. I only needed radio buttons at the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment