Long story short, i'm creating a module that allows me to add newattributes in the customer entity, letting the user insert data in thoseattributes at registration. The problem is that when the new attribute is a boolean the data provided by the user doesn't get saved.
So, as the title says, i've added some new attributes in my customer entity, one of which is a boolean, as you can see from the code in setup/InstallData.php
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'azienda', [ 'type' => 'int', 'label' => 'Azienda', 'input' => 'boolean', 'source' => '', 'required' => false, 'visible' => true, 'position' => 333, 'system' => false, 'backend' => '' ]);
and i've also created the frontend layout override for "customer_account_create", everything works fine, other text fields i've added are saved correctly but i can't get it to save in my database this "azienda" attribute which is a boolean. I've tried checkboxes, radiobuttons, textfields with "yes", "true" and "1", whatever i put in that field the boolean attribute will be always unchecked (false) after registration.
Tried to add "default" => 1 in the entity array but it still doesn't save the value as true/checked.
Hope i've explained my issue well enough, i feel like it's something small that i'm doing wrong but i can't figure out what it is. Thank you for your help
Solved! Go to Solution.
OK, i've managed to fix it, in the end it wasn't something hard or impossibile to find.
I did what i should have done from the beginning, check how the "subscribe to newsletter" was being saved and i've noticed that the <input type="checkbox"> had the value="1" property, so i've put it in my checkbox and now it works. Leaving here the code if someone has my same problem.
<div class="field choice newsletter"> <input type="checkbox" name="azienda" title="<?= $block->escapeHtmlAttr(__('Azienda')) ?>" value="1" id="azienda" class="checkbox"> <label for="azienda" class="label"><span><?= $block->escapeHtml(__('Azienda')) ?></span></label> </div>
From what i understand this works because the checkboxes will be added to the post request ONLY if they are checked with the on value but if we add the value="1" property that gets sent instead and when Magento2 receives azienda -> 1 it saves it correctly in my user entity
Hello,
please use below code for same.
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $customerSetup = $this->_customerSetupFactory ->create(['setup' => $setup]); $customerEntity = $customerSetup->getEavConfig() ->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); $attributeSet = $this->_attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); // Add creditlimit attribute $customerSetup->addAttribute(Customer::ENTITY, 'azienda', [ 'type' => 'int', 'label' => 'Azienda', 'input' => 'boolean', 'source' => '', 'value' => 0, 'default' => '', 'required' => false, 'visible' => false, 'user_defined' => true, 'sort_order' => 300, 'position' => 300, 'system' => false, 'is_used_in_grid' => false, 'is_visible_in_grid' => false, 'is_visible' => true, 'is_filterable_in_grid' => false, 'is_searchable_in_grid' => false, ]); $attribute = $customerSetup->getEavConfig() ->getAttribute(Customer::ENTITY, 'azienda') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => [ 'customer_account_create', 'customer_account_edit', 'checkout_register', 'adminhtml_customer' ], ]); $attribute->save();
After upgrade command, please run reindex command as well.
If it will work then mark as solution.
Hi @Sunil Patel,
I've tried your suggestion but still it doesn't work, as you can see from the attached image the registration is correct and the custom fields i added are all correctly saved except the boolean 'azienda'
Here the code of the frontend layout file that gets loaded:
<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"> <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Informazioni Fatturazione') ?></span></legend> <p> <div class="field regulation"> <label for="regulation" class="label"><span><?php /* @escapeNotVerified */ echo __('Azienda') ?></span></label> <div class="control"> <input type="checkbox" name="azienda" id="azienda" title="<?php /* @escapeNotVerified */ echo __('Azienda') ?>" class="checkbox" data-validate="{required:false}"> </div> <label for="regulation" class="label"><span><?php /* @escapeNotVerified */ echo __('Privato') ?></span></label> <div class="control"> <input type="checkbox" name="privato" id="privato" title="<?php /* @escapeNotVerified */ echo __('Privato') ?>" class="checkbox" data-validate="{required:false}"> </div> <label for="regulation" class="label"><span><?php /* @escapeNotVerified */ echo __('piva azienda') ?></span></label> <div class="control"> <input type="text" name="piva_azienda" id="piva_azienda" title="<?php /* @escapeNotVerified */ echo __('piva azienda') ?>" class="input-text" data-validate="{required:false}"> </div> <label for="regulation" class="label"><span><?php /* @escapeNotVerified */ echo __('cf azienda') ?></span></label> <div class="control"> <input type="text" name="cf_azienda" id="cf_azienda" title="<?php /* @escapeNotVerified */ echo __('cf azienda') ?>" class="input-text" data-validate="{required:false}"> </div> <label for="regulation" class="label"><span><?php /* @escapeNotVerified */ echo __('cf privato') ?></span></label> <div class="control"> <input type="text" name="cf_privato" id="cf_privato" title="<?php /* @escapeNotVerified */ echo __('cf privato') ?>" class="input-text" data-validate="{required:false}"> </div> </div> </p> </fieldset>
Attached screenshot:
Hello @daniel_bertagnolli
Checkbox value sends Yes and No value, can you please check in post and pass 0 and 1 value.
Hope it will help you.
If it will help you then mark as solution.
OK, i've managed to fix it, in the end it wasn't something hard or impossibile to find.
I did what i should have done from the beginning, check how the "subscribe to newsletter" was being saved and i've noticed that the <input type="checkbox"> had the value="1" property, so i've put it in my checkbox and now it works. Leaving here the code if someone has my same problem.
<div class="field choice newsletter"> <input type="checkbox" name="azienda" title="<?= $block->escapeHtmlAttr(__('Azienda')) ?>" value="1" id="azienda" class="checkbox"> <label for="azienda" class="label"><span><?= $block->escapeHtml(__('Azienda')) ?></span></label> </div>
From what i understand this works because the checkboxes will be added to the post request ONLY if they are checked with the on value but if we add the value="1" property that gets sent instead and when Magento2 receives azienda -> 1 it saves it correctly in my user entity