2

Hi I have user registration form with multiple custom fields. They all work fine except for the field I have as a checkbox. It returns an error of 'my-field' is invalid. Any ideas anyone? code below (field in question is towards the bottom)...

<form method="post" accept-charset="UTF-8" class="login-form">
            {{ getCsrfInput() }}
            <input type="hidden" name="action" value="users/saveUser">
            <input type="hidden" name="redirect" value="{{ url('collection/') }}">

            <h3>Register</h3>


            <p>To browse our collection please register your information below.</p>

            <p>Already registered? <a href="#login-modal" class="toggle-modal-trigger">Login</a>.</p>

            {{ entry.contentBeforeForm }}

            {% macro errorList(errors) %}
                {% if errors %}
                    <ul class="errors">
                        {% for error in errors %}
                            <li>{{ error }}</li>
                        {% endfor %}
                    </ul>
                {% endif %}
            {% endmacro %}

            {% from _self import errorList %}



            <h4><label for="firstName">First name</label></h4>
            <input id="firstName" type="text" name="firstName" {%- if account is defined %} value="{{ account.firstName }}"{% endif %} placeholder="First name">
            {{ account is defined and account ? errorList(account.getErrors('firstName')) }}

            <h4><label for="lastName">Last name</label></h4>
            <input id="lastName" type="text" name="lastName" {%- if account is defined %} value="{{ account.lastName }}"{% endif %} placeholder="Last name"> 
            {{ account is defined and account ? errorList(account.getErrors('lastName')) }} 

            <h4><label for="company">Company</label></h4>
            <input id="company" type="text" name="fields[company]" {%- if account is defined %} value="{{ account.company }}"{% endif %} placeholder="Company">  
            {{ account is defined and account ? errorList(account.getErrors('company')) }} 

            <h4><label for="phone">Phone</label></h4>
            <input id="phone" type="text" name="fields[phone]" {%- if account is defined %} value="{{ account.phone }}"{% endif %} placeholder="Phone">
            {{ account is defined and account ? errorList(account.getErrors('phone')) }}

            <h4><label for="username">Username</label></h4>
            <input id="username" type="text" name="username" {%- if account is defined %} value="{{ account.username }}"{% endif -%} placeholder="Username">

            {% if account is defined %}
                {{ errorList(account.getErrors('username')) }}
            {% endif %}

            <h4><label for="email">Email</label></h4>
            <input id="email" type="text" name="email" {%- if account is defined %} value="{{ account.email }}"{% endif %} placeholder="Email">

            {% if account is defined %}
                {{ errorList(account.getErrors('email')) }}
            {% endif %}


            <h4><label for="password">Password</label></h4>
            <input id="password" type="password" name="password">

            {% if account is defined %}
                {{ errorList(account.getErrors('password')) }}
            {% endif %}

            {# HERE IS THE FIELD CAUSING THE ERROR #}
            <label>
            <input type="checkbox" id="termsAgreement" name="fields[termsAgreement]" {%- if account is defined %}value="1" checked{% endif %}>
             I agree to terms and conditions above
            </label>
            {{ account is defined and account ? errorList(account.getErrors('termsAgreement')) }}

            <input type="submit" value="Register">

        </form>
1
  • 1
    Is it really a checkbox field or a lightswitch field? Because checkboxes must be defined as an array. If you are doing it for a lightswitch field you have to pass either 1/true or 0/false. Currently you are sending a null value if the field is not checked as well Commented Apr 6, 2018 at 10:54

1 Answer 1

4

Just in case anyone else has this issue i got it working with these modifications.

I think the hidden field was required for the possibility of null values.

<input type="hidden" name="fields[myCustomField]" value="no">

It also required the extra [] brackets after the field name in the checkbox option (as checkbox fields are an array).

name="fields[myCustomField][]"

Here's my code thats working:

            <input type="hidden" name="fields[termsAgreement]" value="no">
            <input type="checkbox" id="termsAgreement" name="fields[termsAgreement][]" value="yes">

             I agree to terms and conditions above
            </label>
            {{ account is defined and account ? errorList(account.getErrors('termsAgreement')) }}
1
  • This helps, thanks. First had an empty value like <input type="hidden" name="fields[termsAgreement]" value="">, but that didn't force the error. It needs to be a value that is not listed in the field settings.
    – noregt
    Commented Mar 26, 2019 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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