1

I have created a custom module, and added event in etc/events.xml as below :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_account_create">
        <observer name="customizecustomer_accountcreate" instance="Module\Custom\Model\Accountcreate" shared="false" />
    </event>
</config>

And created a file Model/Accountcreate.php as below :

namespace Module\Custom\Model;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class Accountcreate implements ObserverInterface
{

    public function execute(Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/customer.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Accountcreate'); // to check if event is working or not

    }
}

Also executed below commands :

php bin/magento cache:clean
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy

But when I'm trying to register as customer the event is not calling.

I want to add some server side validation on customer account create.

Am I doing anything wrong? Is there any another event which trigger before account create?

1
  • Check my answer it will help you. May 22, 2018 at 5:38

2 Answers 2

5

There is no event customer_account_create

You can use customer_register_success for customer register success.

You can see all the event list here.

Or

You can use this event for validation.

controller_action_predispatch_customer_account_createpost

Magento is using this event for validating captcha.

4
  • I want to add some custom validation before customer account create with the data what customer enter, and don't allow to register if validation failed. May 22, 2018 at 5:43
  • @VinayaMaheshwari You can create your one custom event. May 22, 2018 at 5:57
  • @VinayaMaheshwari check my updated answer. May 22, 2018 at 6:03
  • I have added controller_action_predispatch_customer_account_createpost and also validation on form fields but how to throw exception if validation failed? May 22, 2018 at 6:45
1

Instead of overriding, you can create a custom plugin (the new concept introduced in Magento 2).

Ref. Links:

Benefits of using plugins instead of overriding functions:

  • “Listen” to any method call made on an object manager controlled object and take programmatic action
  • Change the return value of any method call made on an object manager controlled object
  • Change the arguments of any method call made to an object manager controlled object

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.