I have this code for image attribute (avatar).

app/code/Ibnab/CustomerPut/Setup/InstallData.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;

        $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');
        $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);




<?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">
        <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="form_additional_info_customerput" template="Ibnab_CustomerPut::additionalinfocustomer.phtml"/>

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">
        <referenceContainer name="content">
         <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template"  template="Ibnab_CustomerPut::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">

but I am always get message :

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

enter image description here

Please help ..


1 Answer 1


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": "[email protected]", "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
    Commented Jul 25, 2017 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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