Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkbox property not updating #67

Open
ssi-com-pl opened this issue Sep 23, 2024 · 4 comments
Open

Checkbox property not updating #67

ssi-com-pl opened this issue Sep 23, 2024 · 4 comments
Assignees
Labels

Comments

@ssi-com-pl
Copy link

In buddies/properties I added 'accept-terms' chceckbox. In my theme I use this code to update profile fields:

{% set obUser = UserData.get %}

{% if obUser.isNotEmpty %}
    <form class="ajaxForm form--changedata" href="{{ 'profile/edit'|page }}">
        <div class="row">
            <div class="col-12 col-md-4">
                {% partial 'components/input' label="Name" name="name" type="text" value=obUser.name %}
                {% partial 'components/input' label="Surname" name="last_name" type="text" value=obUser.last_name %}
                {% partial 'components/input' label="Phone" name="phone" type="text" value=obUser.phone %}
            </div>
            <div class="col-12 col-md-4">
                {% partial 'components/input' label="Street" name="property[street]" type="text" value=obUser.property['street'] %}
                {% partial 'components/input' label="City" name="property[city]" type="text" value=obUser.property['city'] %}
                {% partial 'components/input' label="Country" name="property[country]" type="text" value=obUser.property['country'] %}
            </div>
            <div class="col-12 col-md-4">
                {% partial 'components/input' label="Terms" name="property[accept-terms]" type="checkbox" value=obUser.property['accept-terms'] checked=obUser.property['accept-terms'] %}
            </div>
        </div>
        <div>
            <button class="button button--changedata" type="submit">Save</button>
        </div>
    </form>
{% else %}
    {% partial 'user/login' %}
{% endif %}

Here is input partial:

<div class="input input--{{type}}">
    {% if type=='radio' or type=='checkbox' %}
        <label class="input__label input__label--{{type}}" for="{{ id ? id : name }}">{{ label | raw }}
            <input class="input__item"
                   type="{{ type }}"
                   id="{{ id ? id : name }}"
                   name="{{ name }}"
                   value="{{ value }}"
                   {% if checked %}checked="checked"{%endif%}
                    {% if required %}
                        required="required"
                    {% endif %}
            >
            <span class="input__checkmark input__checkmark--{{ type }}"></span>
        </label>
    {% elseif type=='textarea' %}
        <label class="input__label" for="{{ id ? id : name }}">{{ label }}</label>
        <textarea class="input__item" type="{{ type }}" rows="4" id="{{ id ? id : name }}" name="{{ name }}" value="{{ value }}"></textarea>
    {% else %}
        <label class="input__label" for="{{ id ? id : name }}">{{ label }}</label>
        <input class="input__item" type="{{ type }}" id="{{ id ? id : name }}" name="{{ name }}" value="{{ value }}">
    {% endif %}
</div>

Text fields work great, but checkbox field don't - checked value are not saved. What am I doing wrong?

@kharanenka
Copy link
Contributor

Hi. What JSON object in ajax request do you have?

@kharanenka kharanenka self-assigned this Sep 23, 2024
@ssi-com-pl
Copy link
Author

When the checkbox is unchecked by default and I check it:
image
When the chcebkox is checked by default (checked via backend) and I uncheck it:
image

@kharanenka
Copy link
Contributor

You have "0" value in property['accept-terms']
You have condition in twig:

checked=obUser.property['accept-terms']

checked=0 - it is value from database in next time

{% if checked %}checked="checked"{%endif%}

{% if checked %} equal false, if "checked" equal "0"

You should change value of your checkboxes

@ssi-com-pl
Copy link
Author

This code appears to work correctly:

<input type="hidden" name="property[accept-terms]" value="0">
{% partial 'components/input' 
     label="Terms" 
     name="property[accept-terms]" 
     type="checkbox" 
     value="1" 
     checked=(obUser.property['accept-terms'] == '1') %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants