The function validateBeforeSave() on the Magento\Framework\Model\AbstractModel gives an error notice when fired "Notice: Array to string conversion in /vendor/magento/framework/Model/AbstractModel.php on line 694" while having a multi-dimensional array for messages.
The correct validation errors are shown individually and no duplicates are shown.
An error notice is given "Notice: Array to string conversion in /vendor/magento/framework/Model/AbstractModel.php on line 694"
$errors = $validator->getMessages() is an array, but in the implode() function it is expected that it is a simple array, however since the messages are added through array_merge_recursive() two messages for key 'invalid_email_format' are added making it a multi-dimensional array.
So its:
@stephan-cream, thank you for your report.
We've created internal ticket(s) MAGETWO-81118 to track progress on the issue.
I am working on it at #mm17es
I am working on it at #mm17es
Hi @akolpakovgor. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.3-develop branchDetails
- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. Verify that the issue is reproducible on 2.2-develop branch. Details
- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x
[ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.
Hi @engcom-Charlie. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.3-develop branchDetails
- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
@stephan-cream, thank you for your report.
Unfortunately, we are archiving this ticket now as it did not get much attention from both Magento Community and Core developers for an extended period. This is done in an effort to create a quality, community-driven backlog which will allow us to allocate the required attention more easily.
Please feel free to comment, reopen or create new ticket according to the Issue reporting guidelines
if you are still facing this issue on the latest 2.4-develop branch. Thank you for collaboration.
@magento-engcom-team You can't just close reproduced and validated issues!
Hi @engcom-Charlie. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.
[ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
[ ] 4. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 5. Add label Issue: Confirmed once verification is complete.
[ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
Hello @dverkade
I can't reproduce this issue on the latest Magento 2.4-develop
Used customer data:

Code(not all):

Result:

Preconditions
Any version of Magento 2 up to 2.3.2 (untested on 2.3.3 and above)
Steps to reproduce
In other words:
try { // First attempt
$this->quoteRepository->save($quote);
} catch (\Exception $exception) {
try { // Second attempt
$this->quoteRepository->save($quote);
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
}
}
Basically save again within the catch(...) {} part of the code.
Hopefully this clarifies how to test this.
Hello @stephan-cream
This issue is reproducible on the Magento 2.3-develop(closed), but not on the 2.4-develop
I still can't reproduce this issue with your update:
<?php
namespace Vendor\Module\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Quote extends Command
{
protected function configure()
{
$this->setName('quote:create');
$this->setDescription('Demo command line');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$store = $storeManager->getStore();
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'John';
$lastName = 'Doe';
$email = 'doe.r@';
$password = 'Test@123';
$address = [
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => [
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
],
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1,
];
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
if (!$customer->getId()) {
try {
$customer = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('\Magento\Customer\Model\AddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
$customAddress->save();
} catch (Exception $e) {
echo $e->getMessage();
}
}
$customer = $objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface')->getById($customer->getId());
try {
$quoteFactory = $objectManager->get('\Magento\Quote\Model\QuoteFactory')->create();
$quoteFactory->setStore($store);
$quoteFactory->setCurrency();
$quoteFactory->assignCustomer($customer);
$productIds = [337 => 2, 338 => 3];
foreach ($productIds as $productId => $qty) {
$product = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->getById($productId);// get product by product id
$quoteFactory->addProduct($product, $qty); // add products to quote
}
/*
* Set Address to quote
*/
$quoteFactory->getBillingAddress()->addData($address);
$quoteFactory->getShippingAddress()->addData($address);
/*
* Collect Rates and Set Shipping & Payment Method
*/
$shippingAddress = $quoteFactory->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
$quoteFactory->setPaymentMethod('checkmo'); //payment method
$quoteFactory->setInventoryProcessed(false);
$quoteFactory->save();
/*
* Set Sales Order Payment
*/
$quoteFactory->getPayment()->importData(['method' => 'checkmo']);
/*
* Collect Totals & Save Quote
*/
$quoteFactory->collectTotals()->save();
/*
* Create Order From Quote
*/
$order = $objectManager->get('\Magento\Quote\Model\QuoteManagement')->submit($quoteFactory);
$order->setEmailSent(0);
$output->writeln('Order Id:' . $order->getRealOrderId());
} catch (Exception $e) {
$output->writeln($e->getMessage());
}
try {
$quoteFactory = $objectManager->get('\Magento\Quote\Model\QuoteFactory')->create();
$quoteFactory->setStore($store);
$quoteFactory->setCurrency();
$quoteFactory->assignCustomer($customer);
$productIds = [337 => 2, 338 => 3];
foreach ($productIds as $productId => $qty) {
$product = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->getById($productId);// get product by product id
$quoteFactory->addProduct($product, $qty); // add products to quote
}
/*
* Set Address to quote
*/
$quoteFactory->getBillingAddress()->addData($address);
$quoteFactory->getShippingAddress()->addData($address);
/*
* Collect Rates and Set Shipping & Payment Method
*/
$shippingAddress = $quoteFactory->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
$quoteFactory->setPaymentMethod('checkmo'); //payment method
$quoteFactory->setInventoryProcessed(false);
$quoteFactory->save();
/*
* Set Sales Order Payment
*/
$quoteFactory->getPayment()->importData(['method' => 'checkmo']);
/*
* Collect Totals & Save Quote
*/
$quoteFactory->collectTotals()->save();
/*
* Create Order From Quote
*/
$order = $objectManager->get('\Magento\Quote\Model\QuoteManagement')->submit($quoteFactory);
$order->setEmailSent(0);
$output->writeln('Order Id:' . $order->getRealOrderId());
} catch (Exception $e) {
$output->writeln($e->getMessage());
}
}
}
Result:

@stephan-cream i skipped creation of new customer and just changed customer's email in the database and just load customer

<?php
namespace Module\Vendor\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Quote extends Command
{
protected function configure()
{
$this->setName('quote:create');
$this->setDescription('Demo command line');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$store = $storeManager->getStore();
$websiteId = 1;
$firstName = 'John';
$lastName = 'Doe';
$address = [
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => [
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
],
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1,
];
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
$customer = $objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface')->getById(1);
try {
$quoteFactory = $objectManager->get('\Magento\Quote\Model\QuoteFactory')->create();
$quoteFactory->setStore($store);
$quoteFactory->setCurrency();
$quoteFactory->assignCustomer($customer);
$productIds = [1 => 1];
foreach ($productIds as $productId => $qty) {
$product = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->getById($productId);// get product by product id
$quoteFactory->addProduct($product, $qty); // add products to quote
}
/*
* Set Address to quote
*/
$quoteFactory->getBillingAddress()->addData($address);
$quoteFactory->getShippingAddress()->addData($address);
/*
* Collect Rates and Set Shipping & Payment Method
*/
$shippingAddress = $quoteFactory->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
$quoteFactory->setPaymentMethod('checkmo'); //payment method
$quoteFactory->setInventoryProcessed(false);
$quoteFactory->save();
/*
* Set Sales Order Payment
*/
$quoteFactory->getPayment()->importData(['method' => 'checkmo']);
/*
* Collect Totals & Save Quote
*/
$quoteFactory->collectTotals()->save();
/*
* Create Order From Quote
*/
$order = $objectManager->get('\Magento\Quote\Model\QuoteManagement')->submit($quoteFactory);
$order->setEmailSent(0);
$output->writeln('Order Id:' . $order->getRealOrderId());
} catch (Exception $e) {
$output->writeln($e->getMessage());
}
try {
$quoteFactory = $objectManager->get('\Magento\Quote\Model\QuoteFactory')->create();
$quoteFactory->setStore($store);
$quoteFactory->setCurrency();
$quoteFactory->assignCustomer($customer);
$productIds = [1 => 1];
foreach ($productIds as $productId => $qty) {
$product = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->getById($productId);// get product by product id
$quoteFactory->addProduct($product, $qty); // add products to quote
}
/*
* Set Address to quote
*/
$quoteFactory->getBillingAddress()->addData($address);
$quoteFactory->getShippingAddress()->addData($address);
/*
* Collect Rates and Set Shipping & Payment Method
*/
$shippingAddress = $quoteFactory->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
$quoteFactory->setPaymentMethod('checkmo'); //payment method
$quoteFactory->setInventoryProcessed(false);
$quoteFactory->save();
/*
* Set Sales Order Payment
*/
$quoteFactory->getPayment()->importData(['method' => 'checkmo']);
/*
* Collect Totals & Save Quote
*/
$quoteFactory->collectTotals()->save();
/*
* Create Order From Quote
*/
$order = $objectManager->get('\Magento\Quote\Model\QuoteManagement')->submit($quoteFactory);
$order->setEmailSent(0);
$output->writeln('Order Id:' . $order->getRealOrderId());
} catch (Exception $e) {
$output->writeln($e->getMessage());
}
}
}
It is fine if it is fixed in 2.4.0 but we have no idea when this will be released.
My bug report was not only placed on 2017 but also refers to Magento versions up to 2.3.2.
If there has been a fix released for this but it won't be released for current released versions, we would at least like to know what that fix is so we can implement a patch for this until 2.4.0 is released.
Furthermore your test differs from my description so i can't be sure if it is actually fixed.
See my test (this has to be tested):
$quote = $this->quoteFactory->create(); // or you can do this with an existing one
...
try { $this->quoteRepository->save($quote); }
catch () { $this->quoteRepository->save($quote); // 2nd save for the same quote }
Your test (which is different):
$quote = $this->quoteFactory->create();
try { $quote->collectTotals()->save(); }
catch { // log error}
$quote = $this->quoteFactory->create();
try { $quote->collectTotals()->save(); }
catch { // log error }
@stephan-cream
Is your quoteRepository using CartRepositoryInterface?
I tried again with your new update and result is same as before:

<?php
namespace Mod\HelloWorld\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Sayhello extends Command
{
protected function configure()
{
$this->setName('quote:create');
$this->setDescription('Demo command line');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$store = $storeManager->getStore();
$websiteId = 1;
$firstName = 'John';
$lastName = 'Doe';
$address = [
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => [
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
],
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1,
];
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
$customer = $objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface')->getById(1);
try {
$quoteFactory = $objectManager->get('\Magento\Quote\Model\QuoteFactory')->create();
$quoteFactory->setStore($store);
$quoteFactory->setCurrency();
$quoteFactory->assignCustomer($customer);
$productIds = [1 => 1];
foreach ($productIds as $productId => $qty) {
$product = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->getById($productId);// get product by product id
$quoteFactory->addProduct($product, $qty); // add products to quote
}
/*
* Set Address to quote
*/
$quoteFactory->getBillingAddress()->addData($address);
$quoteFactory->getShippingAddress()->addData($address);
/*
* Collect Rates and Set Shipping & Payment Method
*/
$shippingAddress = $quoteFactory->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
$quoteFactory->setPaymentMethod('checkmo'); //payment method
$quoteFactory->setInventoryProcessed(false);
$quoteFactory->save();
/*
* Set Sales Order Payment
*/
$quoteFactory->getPayment()->importData(['method' => 'checkmo']);
/*
* Collect Totals & Save Quote
*/
$quoteFactory->collectTotals()->save();
/*
* Create Order From Quote
*/
$order = $objectManager->get('\Magento\Quote\Model\QuoteManagement')->submit($quoteFactory);
$order->setEmailSent(0);
$output->writeln('Order Id:' . $order->getRealOrderId());
} catch (Exception $e) {
$quoteFactory->collectTotals()->save();
}
}
}
@engcom-Charlie
Apparently it no longer is an issue in 2.4.0.
But as i said before:
It is fine if it is fixed in 2.4.0 but we have no idea when this will be released.
My bug report was not only placed on 2017 but also refers to Magento versions up to 2.3.2.
If there has been a fix released for this but it won't be released for current released versions, we would at least like to know what that fix is so we can implement a patch for this until 2.4.0 is released.
We faced this problem in Magento 2.3.5-p2, trying to create a new order from the backend.
We fixed it creating a custom patch to the magento/framework module _Magento\Framework\Model\AbstractModel::validateBeforeSave()_ with this code:
/**
* Validate model before saving it
*
* @return $this
* @throws \Magento\Framework\Validator\Exception
*/
public function validateBeforeSave()
{
$validator = $this->_getValidatorBeforeSave();
if ($validator && !$validator->isValid($this)) {
$errors = $validator->getMessages();
// Handle associative array messages. Fix issue with backend order email validation
$errors = array_values($errors);
foreach ($errors as $key => $value) {
if (is_array($value)) {
// Prevent duplicate messages
$value = array_unique($value);
$errors[$key] = implode(PHP_EOL, $value);
}
}
// End error message array customization
$exception = new \Magento\Framework\Validator\Exception(
new Phrase(implode(PHP_EOL, $errors))
);
foreach ($errors as $errorMessage) {
$exception->addMessage(new \Magento\Framework\Message\Error($errorMessage));
}
throw $exception;
}
return $this;
}
Hope this helps somebody else