3

I need add an errorMessage or successMessage on checkout/cart after an jquery validation. I tried require file 'Magento_Ui/js/model/messageList' for this but not have success.

My code:

define(
    [
        'jquery',
        'Magento_Ui/js/model/messageList'
    ],
    function ($, messageList) {
       $.ajax({
           type: "GET",
           url: /route/controller,
           async: false,
           success: function (res) {
               if (res.val) {
                  messageList.addErrorMessage({'message': messageError});
               }
           }
       });
    }
);

3 Answers 3

1

I'm not sure if you already solved this or not, but here it goes anyway. While Rakesh might be right (a controller is the preferred way), it doesn't answer the question. I found the easiest way of doing this is through the customerData object:

customerData.set('messages', {
    messages: [{
        type: 'success',
        text: 'Hello World'
    }]
});
1

I did this approach for after page load message display:

     define(['uiRegistry', 'mage/translate'], function(uiRegistry, $t) {
         let msgComponent = uiRegistry.get('index = messages');
         msgComponent.messages().messages = [];
         msgComponent.messages().messages.push({
            type: 'warning',
            text: $t('You are not allowed to edit this user.')
          });
         msgComponent.messages.valueHasMutated();
     });

You can use type as 'error', 'warning', 'success', etc...

0

Add success message in your controller it will automatically display in your page.

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.