# CustomField als Checkbox in Frontend-Formular

**URL:** https://forum.shopware.com/t/customfield-als-checkbox-in-frontend-formular/104209
**Category:** Allgemein
**Created:** [6. Juni 2024 um 08:52 UTC](https://forum.shopware.com/t/customfield-als-checkbox-in-frontend-formular/104209 "2024-06-06T08:52:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Frank\_2812](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@Frank\_2812](https://forum.shopware.com/u/Frank_2812)
#### Post date: [6. Juni 2024 um 08:52 UTC](https://forum.shopware.com/t/customfield-als-checkbox-in-frontend-formular/104209/1 "2024-06-06T08:52:43Z")

</div>

Hallo,

ich möchte ein CustomField, welches am Kunden hängt, in den Profil-Einstellungen hinzufügen:

> **[Add Custom Field in the Storefront | Shopware Documentation](https://developer.shopware.com/docs/guides/plugins/plugins/storefront/using-custom-fields-storefront.html)**
>
> Documentation for Shopware developers

Dafür habe ich das Template …/storefront/component/address/address-personal.html.twig entsprechend erweitert. Mit einem Textfeld (custom\_clients\_test) funktioniert dass, wie in der Doku beschrieben problemlos:

```auto
<div class="form-group col-12">
     <label class="form-label" for="customFields[custom_clients_test]">
         {{ "customFields.custom_clients_test"|trans|sw_sanitize }}
     </label>
     <input type="text"
         class="form-control"
         name="customFields[custom_clients_test]"
         value="{{context.customer.customFields['custom_clients_test'] }}"
         id="customFields[custom_clients_test]">
</div>

```

Ich benötige das Ganze aber für eine Checkbox (custom\_clients\_name\_on\_invoice):

```auto
<div class="form-group col-12">
     <input type="checkbox" 
         class="form-check-input"
         name="customFields[custom_clients_name_on_invoice]"
         value="{{context.customer.customFields['custom_clients_name_on_invoice'] }}"
         id="customFields[custom_clients_name_on_invoice]">
     <label class="form-label" for="customFields[custom_clients_name_on_invoice]">
         {{ "customFields.custom_clients_name_on_invoice"|trans|sw_sanitize }}
     </label>
</div>

```

Die Datenanbindung der Checkbox an das CustomField wird komplett ignoriert.  
Geht das prinzipiell nicht, oder mache ich mit eventuell mit den Klassen was falsch?

---

<div class="post-metadata">

### Author: ![Frank\_2812](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@Frank\_2812](https://forum.shopware.com/u/Frank_2812)
#### Post date: [8. Juni 2024 um 12:44 UTC](https://forum.shopware.com/t/customfield-als-checkbox-in-frontend-formular/104209/2 "2024-06-08T12:44:12Z")

</div>

Hallo,

nach einigen Versuchen bin ich nun zu der Erkenntnis gekommen, dass die o.g. Methode nur bei Textfeldern funktioniert und leider nicht bei Checkboxen.

Habe es jetzt mittels folgendem Subscriber gelöst:

```auto
public static function getSubscribedEvents()
{
    return [
        CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'setCustomField'
    ];
}

public function setCustomField(DataMappingEvent $event): void
{
    $outputData = $event->getOutput();
    $this->register_name_on_invoice = ($event->getInput()->getBoolean('invoice') ? true : false);
    $outputData['customFields']['custom_clients_name_on_invoice'] = $this->register_name_on_invoice;
    $event->setOutput($outputData);
}

```

Im Twig-Template sieht das Ganze dann so aus:

```auto
<div class="form-check">
    <input type="checkbox"
        class="form-check-input"
        id="invoice"
        name="invoice"
        {% if context.customer.customFields['custom_clients_name_on_invoice'] %}checked="checked"{% endif %}>
                
    <label class="custom-control-label no-validation" for="invoice">
        {{ "account.noNameInvoiceCheckboxLabel"|trans|sw_sanitize }}
    </label>
 </div>

```

Ein wenig schade, da man den Subscriber sparen könnte, aber leider offenbar nicht, wenn Checkboxen zum Einsatz kommen 🤔.

---

<div class="post-metadata">

### Author: ![system](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/2/9/29c7587ba660f00e4995d1743162782e5d6d8653.svg) [@system](https://forum.shopware.com/u/system)
#### Post date: [8. Juli 2024 um 12:44 UTC](https://forum.shopware.com/t/customfield-als-checkbox-in-frontend-formular/104209/3 "2024-07-08T12:44:19Z")

</div>

Dieses Thema wurde automatisch 30 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.
