1

I've got a frontend user registration form working that's populating a whole set of custom fields I've set up on my user profiles in Craft.

But I've got a set of lightswitch fields on my user profiles that I'm trying to set via checkboxes in the registration form but the values are not transferring over when submitting the form.

Here's an example of my checkbox code in the frontend form (the corresponding lightswitch field handle is memberShowEmailLink):

<div class="checkbox">
  <label>
    <input type="checkbox" id="memberShowEmailLink" name="fields[memberShowEmailLink]" {% if account is defined %}value="{{ account.memberShowEmailLink }}" checked="checked"{% else %}value="0"{% endif %}>
    Show a link to contact me via email in the member directory?
  </label>
</div>

Is there a way to change it to make this work?

1

When the checkbox get submitted, it get's transferred with whatever value is set with the value attribute.

Thus, your need to set the value to 1. Your code should be more like this:

<div class="checkbox">
  <label>
    <input type="checkbox" id="memberShowEmailLink" name="fields[memberShowEmailLink]" value="1" {% if account is defined and account.memberShowEmailLink %} checked="checked"{% endif %}>
    Show a link to contact me via email in the member directory?
  </label>
</div>
3
  • I'd like these checkboxes to be off by default, which is why I set the value="0" in the else bit of my conditional. But when I check them on in the frontend, the value is not getting transferred. Jan 8 '15 at 16:52
  • They are "off" by default. The checked attribute controls if they are "on". If they aren't checked, the field isn't transferred to the server. Jan 8 '15 at 16:53
  • Got it! Tried the code you revised and it worked great. Thanks for clarifying! Jan 8 '15 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.