0

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.

Hope i've explained my issue well enough, thank you for your help :)

  • did you try to put default = 1, ['default' => 1] in your array? – WISAM HAKIM Apr 19 '18 at 17:44
  • @WISAMHAKIM i've tried but it still saves it as false/unchecked – Daniel Apr 20 '18 at 7:03
  • may be it is better to reverse the logic, it will be more easier, for example instead of (show in frontend), you can put (hide in frotnend). in this way you can keep the false as default value – WISAM HAKIM Apr 21 '18 at 4:12
  • Hi @WISAMHAKIM i managed to fix it without changing the logic, still thank you for your help. I'm adding how i fixed it as an answer – Daniel Apr 24 '18 at 12:38
1

I have managed to fix it, in the end it wasn't something hard or impossible 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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