7

I would like to use special characters as a key in a Name Value List field. Any idea why Sitecore doesn't allow this? Is there any nice way to disallow this validation?

enter image description here

2 Answers 2

10

The Name Value List (and Name Value Lookup List which also inherits from it) only allows keys containing letters and numbers due to the Validate() method in Sitecore.Shell.Applications.ContentEditor.NameValue:

if (!string.IsNullOrEmpty(input) && !Regex.IsMatch(input, "^\\w*$"))
{
  SheerResponse.Alert(string.Format("The key \"{0}\" is invalid.\n\nA key may only contain letters and numbers.", (object) input));
  SheerResponse.SetReturnValue(false);
  return false;
}

Whilst the answer from @alan is correct that the raw data is stored in parameters, the field is able to handle special characters and I have created fields that use a guid as the key (which includes dashes and curly braces). You may want to ensure that values are URL encoded just in case though.

In any case, there are no settings available to change this behvaiour, you have to create a custom field. Unfortunately the Validate() method is private so it's not just a simple case of overriding, but you could provide a new implementation of ParameterChange() method which is where Validate is called from.

protected new void ParameterChange()
{
    ClientPage clientPage = Sitecore.Context.ClientPage;
    if (clientPage.ClientRequest.Source == StringUtil.GetString(clientPage.ServerProperties[this.ID + "_LastParameterID"]) && !string.IsNullOrEmpty(clientPage.ClientRequest.Form[clientPage.ClientRequest.Source]))
    {
        string str = this.BuildParameterKeyValue(string.Empty, string.Empty);
        clientPage.ClientResponse.Insert(this.ID, "beforeEnd", str);
    }
    NameValueCollection form = (NameValueCollection)null;
    System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
    if (page != null)
        form = page.Request.Form;
    if (form == null) /* || !this.Validate(form)) -- removed validation of Key field */
        return;
    clientPage.ClientResponse.SetReturnValue(true);
}
3
  • Thanks @jammykam. I would like to publish the exact solution when I am done. I will put it here. Commented Oct 7, 2016 at 6:58
  • 2
    I wrote a blogpost about the exact solution. You can check it here - trnktms.wordpress.com/2016/10/15/… Commented Oct 15, 2016 at 10:29
  • 3
    Hey @TRNKTMS, I suggest linking back to this page on your blog post and also giving credit to Kamruz. The blog post as it stands makes it appear you solved this on your own which is not the case. Commented Oct 15, 2016 at 11:18
0

I think it might be forbidden as raw value of the field is 'url like' format thus some special characters are forbidden.

Normally you resolve field value in code like this:

Sitecore.Web.WebUtil.ParseUrlParameters(item.Fields['NameValueListField']?.Value);

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.