0

I need help. I am creating a custom payment method for Magento 2. In the plugin, user needs to enter the keys. While saving the config, I want to check if the keys entered by the user are correct or not. If the keys are incorrect, I want to show a message informing user about the issue.

I have googled about it and I thought I have found the event admin_system_config_changed_section_{sectionname}. I implemented it, cleared the cache and recompiled the code but when I save the config data for the payment method plugin, it does not trigger the class. I looked into Magento documentation. I could not find the exact full event name for it. These are the full event name I tried:

admin_system_config_changed_section_payment_methods admin_system_config_changed_section_paymentmethods admin_system_config_changed_section_sales

MyPayment/Payment/etc/adminhtml/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="admin_system_config_changed_section_payment_methods">
        <observer name="my_payment_admin_system_config_changed_section_paymentmethods" instance="MyPayment\Payment\Observer\CustomPaymentMethodConfigObserver" />
    </event>
</config>

MyPayment/Payment/Observer/CustomPaymentMethodConfigObserver

<?php

namespace MyPayment\Payment\Observer;

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

class CustomPaymentMethodConfigObserver implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        // some codes that did not trigger
    }
}
IamGhale
  • 1,275
  • 2
  • 14
  • 26

1 Answers1

0

Try using a <backend_model>

public function beforeSave()
{
    if ($this->getValue() == '') {
        throw new \Magento\Framework\Exception\ValidatorException(__($label . ' is required.'));
    } else ($this->getValue())) {
         //custom validation here
    }

    $this->setValue($this->getValue());
    parent::beforeSave();
}

See

MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62