3

I have this code for image attribute (avatar).

app/code/Ibnab/CustomerPut/Setup/InstallData.php :

<?php
namespace Ibnab\CustomerPut\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;





class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;
    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $installer = $setup;
        $installer->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
        $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "avatar");

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "avatar",  array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Avatar",
            "input"    => "image",
            "source"   => "",
            "visible"  => true,
            "required" => true,
            "default" => "",
            "frontend" => "",
            "unique"     => false,
            "note"       => ""

        ));

        $avatar   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "avatar");

        $avatar = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'avatar');
        $used_in_forms[]="adminhtml_customer";
        $used_in_forms[]="checkout_register";
        $used_in_forms[]="customer_account_create";
        $used_in_forms[]="customer_account_edit";
        $used_in_forms[]="adminhtml_checkout";
        $avatar->setData("used_in_forms", $used_in_forms)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 100);

        $avatar->save();

        $installer->endSetup();
    }
}

app/code/Ibnab/CustomerPut/view/frontend/layout/customer_account_create.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="form_additional_info_customerput" template="Ibnab_CustomerPut::additionalinfocustomer.phtml"/>
        </referenceContainer>
    </body>
</page>

app/code/Ibnab/CustomerPut/view/frontend/layout/customer_account_edit.xml :

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
         <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template"  template="Ibnab_CustomerPut::additionalinfocustomer.phtml"/>
         </referenceContainer>

        </referenceContainer>

    </body>
</page>

app/code/Ibnab/CustomerPut/view/frontend/templates/additionalinfocustomer.phtml

   <fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
        <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend><br>

        <div class="field avatar required">
            <label for="avatar" class="label"><span><?php /* @escapeNotVerified */ echo __('Avatar') ?></span></label>
            <div class="control">
                <input type="file" name="avatar" id="avatar" title="<?php /* @escapeNotVerified */ echo __('Avatar') ?>" autocomplete="off">
            </div>
        </div>
    </fieldset>
  • Avatar is shown perfectly in registration page and i can upload an image easily. But I am trying to sign up using rest API as :

    POST http://localhost/magento2/index.php/rest/V1/customers

     customer[email]​ = abc@gmail.com 
     customer[firstname]​ = abc
     customer[lastname]​ = xyz 
     password​ = abc@12345 
     avatar   ms.jpg       =>(file)
    

but I am always get message :

{
    "message": "\"Avatar\" is a required value."
}

enter image description here

Please help ..

0

You need to update the new attribute configuration.

You create this attribute with required to true :

"required" => true,

I do not think that you can make this attribute not required only for REST.

But I think that you can make it required on the store scope.

If you want to make it required with the API, you need to create/update the API endpoint in order to be able to upload this file. Maybe by sending the file encoded in base64 but carefull with security issue.

  • @Frank, Thank you. I tried to make the attribute not required. but it posted the data and didn't upload the image. I think my error is in the avatar attribute: { "customer": { "email": "abc@gmail.com", "firstname": "Abc", "lastname": "Xyz" , "custom_attributes": [ { "attribute_code": "avatar", "value": "" } ] }, "password": "Abc@123456" } ,, How can I encode image ? because the result is { "message": "The image content must be valid base64 encoded data." – Jsparo30 Jul 25 '17 at 14:13

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.