Multiple string values in custom visitor group criterion

Vote:
 

I would like to create a custom visitor group criterion that accepts a list of emails to be part of the group, how would I go about configuring that?

Something like this but there needs to be a step where the input string is split into separate email addresses.

    public class EmailListModel : CriterionModelBase
    {
        [DojoWidget(WidgetType = "dijit.form.Textarea")]
        public string[] Emails { get; set; }
        
        public override ICriterionModel Copy()
        {
            return ShallowCopy();
        }
    }

    [VisitorGroupCriterion(
        Category = "User Criteria",
        Description = "",
        DisplayName = "Email list")]
    public class EmailListCriterion : CriterionBase<EmailListModel>
    {
        private readonly CustomerContext _customerContext;

        public EmailListCriterion()
        {
            _customerContext = ServiceLocator.Current.GetInstance<CustomerContext>();
        }

        public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
        {
            var currentCustomerEmail = _customerContext.CurrentContact?.Email;

            if (string.IsNullOrWhiteSpace(currentCustomerEmail))
                return false;

            return Model.Emails.Contains(currentCustomerEmail);
        }
    }
#249141
Edited, Feb 24, 2021 12:21
Vote:
 

Just use the string split function on the newline character. The textarea will be saving the values as an Environment.NewLine / "/n" I believe so splitting on that should do the job

#249143
Feb 24, 2021 13:12
hjalmarzetterstrom - Feb 24, 2021 13:15
Yes, but where? Is there a step that I can hook into where the value is beeing saved?
If I run the above example I get an error basically saying that the string can't be saved in a string[].
Scott Reed - Feb 24, 2021 13:16
Ah, I think you need to just change the type from String[] to String. The textarea is just a string value. You then split the string on the newline in your IsMatch method
hjalmarzetterstrom - Feb 24, 2021 13:18
I was thinking about that solution as well but thought that it might be too slow? How often is the IsMatch(..) called?
Scott Reed - Feb 24, 2021 13:21
Match on a Criterion is called every time the match is evaluated. A string split is a very low cost piece of code however so there should be no issue using that to split the values performance wise. If you want to store them individually you'd have to use a different dijit component such as the equivalent of a PropertyList but that's a bit overkill IMO
hjalmarzetterstrom - Feb 24, 2021 13:22
Ah okay, thanks!
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.