Developer forum

Forum » Development » Prevent creation of duplicate name for Product Groups

Prevent creation of duplicate name for Product Groups

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

We have a solution running on DW 9.12.3.

We need to create a validation for Product Groups for preventing the creation of Groups with the same name as existing Groups.

For this purpose, we are using a Group.BeforeSave() notification. We are checking the existing names and we are using StopExecution from BeforeSaveArgs.

The execution is indeed prevented, but we get a yellow error in the process:

I am not sure if this is a bug or if we are doing it wrong.

If there is another way to approach this request, I would be happy to hear about it.

Thank you,

Adrian


Replies

 
Nicolai Pedersen
Reply

Show your code please.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Here it is:

[Dynamicweb.Extensibility.Notifications.Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.Group.BeforeSave)]
    public class DfGroupBeforeSaveObserver : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
        {
            if (args == null)
                return;

            if (!(args is Dynamicweb.Ecommerce.Notifications.Ecommerce.Group.BeforeSaveArgs))
                return;

            Dynamicweb.Ecommerce.Notifications.Ecommerce.Group.BeforeSaveArgs  group = (Dynamicweb.Ecommerce.Notifications.Ecommerce.Group.BeforeSaveArgs )args;

            if (group.Group == null)
                return;

            group.StopExecution = true;
            return;
        }
     }

Thank you,

Adrian

 
Nicolai Pedersen
Reply

Yes, you have to do something to display a message. Otherwise only group save will be skipped and the rest of the group save will continue running.

Since this is during creation of a group, the group will end up not existing, giving an error - this is what you see.

So find a way to display the error. I.e. response.write("<script>alert('Group name not allowed');history.go(-1)</script>") or something like that

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

 

Hi Nicolai,

Thank you for the suggestion.

I am afraid though that I don't understand where should I trigger the javascript alert.

I have tried with this:

Dynamicweb.Context.Current.Response.Write("<script>alert('Group name not allowed');history.go(-1);</script>");

And with this:

Dynamicweb.Controls.ControlResources.AddScriptBlock($"KeyGroupAddScriptBlock","<script>alert('Group name not allowed');history.go(-1);</script>");

I have added it right before the 

group.StopExecution = true;

But no matter how I try it, it still gets to that error. 

What am I doing wrong?

Thank you,


Adrian

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Do not set group.StopExecution = true;

But instead after response.write do a response.end.

Votes for this answer: 1
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Works perfectly!

Thank you.
Adrian

 

You must be logged in to post in the forum