I spent many hours recently trying to figure out why a custom Magento 2 customer registration attribute was not working only to find that a relatively simple mistake was the culprit.
The attribute appeared to be created correctly in the eav_attribute
database table, but the frontend value was not being saved to the database when the customer registered.
The attribute was a checkbox, which has a boolean true/false value. As I set the checkbox to be checked by default, my mistake was simply not to set a default value which meant that no value for the custom attribute was being passed via the registration form to the backend customer registration / creation process.
<div class="field gj_custom_boolean_attribute"> <label for="gj_custom_boolean_attribute" class="label"><span><?php /* @escapeNotVerified */ echo __('Customer Custom Boolean Attribute') ?></span> </label> <div class="control"> <input type="checkbox" checked name="gj_custom_boolean_attribute" id="gj_custom_boolean_attribute" title="<?php /* @escapeNotVerified */ echo __('Customer Custom Boolean Attribute') ?>" class="input-text" autocomplete="off" value="1"> </div> </div>
In case anyone else is trying to create a custom checkbox (boolean) attribute and experiencing the same mind numbingly annoying problem here is a module with a demonstration of two custom Magento 2 attributes – checkbox and text input that demonstrates the correct working code.
https://github.com/gaiterjones/Magento2_Gaiterjones_CustomerRegistration
The module creates two attributes:
- gj_custom_boolean_attribute – a sample boolean checkbox attribute
- gj_custom_text_attribute – a sample text attribute
The additional registration html is in the additionalinfocustomer.phtml
template file. Note here that the checkbox requires a default value
value="1"
Comments