Accessing Contextual Data from a Custom Form Field Validator

October 26, 2016

Blog | Development | Accessing Contextual Data from a Custom Form Field Validator

A clean install of Sitecore comes with a number of options for Validation Rules (both on the field level and on the item level) and also allows the ability to create your own. Depending upon the logic within the rule, a developer may have a need to access the currently selected item within the Content Tree. This might be necessary if a different field value from the same item needs to be accessed.

Some Sitecore instances might utilize a content tree structure that requires accessing a different item in relation to the selected, for example, values in an item housing settings may affect validation on the selected item. Whatever the reason you have for exposing the loaded item, information regarding the item may be extracted through the current form in HttpContext.

The retrieved form comes in the shape of a Name Value Collection and may contain a variety of useful information such as current load item, view state, etc. An example is shown in the following excerpt:

 

{__PARAMETERS=LoadItem(%22CA5A53B9518D45D8AE808D8BAD446118%22)&__EVENTTARGET=&__EVENTTARGET=&__EVENTARGUMENT=&__EVENTARGUMENT=&__SOURCE=&__EVENTTYPE=&__CONTEXTMENU=&__MODIFIED=&__ISEVENT=1&__CSRFTOKEN=%2fwEFJDczNmZhYWVlLWNkNWYtNGQ1MS1hOWJlLWQ4YTAwZGVkNDZiMA%3d%3d&__VIEWSTATE=B899770EE58C4AF89A59D4AE0C946A78&__VIEWSTATE=&scLanguage=en&RadSpell=&RadSpell_dialogOpener_Window_ClientState=&RadSpell_dialogOpener_ClientState=&RadSpell_ClientState=&scActiveRibbonStrip=&scEditorTabs=&scActiveEditorTab=&scPostAction=&scShowEditor=&scSections=&ToolTipManager_ClientState=&scChunkCrcs=C4630340E5FCE4A78A6784F8479015AD6%3d2900042031%26C28353D7ACFCA49B5B344C3A97415C89B%3d1690386530%26C373217F6FC5C4BEBB5D9435B8C37D539%3d3842812455%26CB91973ACF8C344B7AB8ED6E3ADF4CF7A%3d4194625066%26CC941DA32F2C6474882774E139B48DF54%3d772193841%26C3FCEBB9F38F4AB4857FCFC415B6A342_42F1FD8820E342BD9217A9468F115928%3d1378731904%26C2CE1C92E23384C6AAE86A742A32A0A6F%3d4292821368%26C5F603BC4710641F8A2753CD6F64C9260%3d3886643319%26D0FC99D95C6543A3ACB6449
5D44FB0E7_42F1FD8820E342BD9217A9468F115928%3d2828689306%26CFE9EE8589F684F2D88D918FC86BFD093%3d198623270%26CC9EBE7DCC053450C803BE99467B455F2%3d3414162399%26C22AEF88C58BC4BFD8F34299782312338%3d2068219641%26CE42470D6C6FC4F618BF7D87CC7A95659%3d2401031259%2692ADE792683F480DA30A01C1F1BAAB53_42F1FD8820E342BD9217A9468F115928%3d4134213231%26C6060CCB1496B4151AA7D961C4495DD6F%3d3109881546%26C7DACFA83AD13451998E66B19BAD3DEC8%3d2881889713%26CBF7AE2C9F7B84E05A4B41331EA6CA378%3d1059345388%26F51BC51AF5AF41DFAD11F0D1B48FFC93_42F1FD8820E342BD9217A9468F115928%3d942623734%26CE0EF7E993CCE4E60BDD71190F2B375F7%3d3051892043%26C9B8B99F35AE84B29A320DFF191C4E3AB%3d2728240279%26CB0E083BE21464CD3BD8F00127E64ED63%3d3764200443%266553636FE9044F2DB222090D0A26298C_42F1FD8820E342BD9217A9468F115928%3d1391456915%26C7524A58D7C044E3A822A4AE6A71F223C%3d1702968090%26CCC085075EEBC463885EE205BE411F0A9%3d1497090190%26C39622ED5931D433EA3BBF0BDDCECD35A%3d989365061%26CE759C8D7C51342D9A9D4163F2F94B0BD%3d1659062210%264CF647168B8C4ABC86C2F9C5F3FF1128_42F1FD88
20E342BD9217A9468F115928%3d2183711924%26CE8FF112B0ED9450DAACC619D22D3F4D0%3d1479607806%26C56E1F418FD654C63B418E44398489591%3d1822504515%26C5EB9993C4B6B46588C9B9900BC0CDB78%3d2196015812%26C1DEF50287025462F9D3C30A12E36E0A7%3d366629119%26CF5D61AF1FE4A45B389EFE463AF62EE30%3d2680243070%26C9F40ED7606BB4AE18DEEB2B7BB55B296%3d1914864689%26C62851F9B9E384465ACFC62E2ADF1B64F%3d2579764063%26D600C13992D748DEBB9F4812A3DB9A8B_42F1FD8820E342BD9217A9468F115928%3d1239083930%26C5ED8240231C74EB98BC8440F61AAB8BE%3d1029981400%26CE68F6E1841304D10A4090D7FC0BFE9BB%3d291147393%26C2E3B47DB9BAC4C7A89EB89C9D32CE20D%3d1669680446%26CF186DEA40D8B44CE9B70315DE281F9CA%3d1731067857%2648967C19C387454D8ED2562DF1409D08_42F1FD8820E342BD9217A9468F115928%3d506257745%26C6652433E49D74549BFD30176BE59D457%3d3760745209%26CAC1F7E7D14F04CB29C69A6E44761423A%3d117505064%26CBDDC1097F60B46CC8E4047AF92ECFF2E%3d3593845050%26AED95E7E874A4CF0924B8DD90FC8D730_42F1FD8820E342BD9217A9468F115928%3d1692779661%26C8787E501F2E9434E9F7645259D9D8C7C%3d1977108982%26336E99C8C45
...

HttpContext.Current.Request.Form Excerpt

 

The key of interest here is __PARAMETERS.

 

 var currentFormParameters = HttpContext.Current.Request.Form["__PARAMETERS"];

Current Form Parameters from within Field Validation Rule

 

In the example above, the form parameters value returned is a string value equivalent to “LoadItem(\”CA5A53B9518D45D8AE808D8BAD446118\”)”. This value contains the unformatted Sitecore item ID for the current LoadItem, or the ID corresponding to the currently selected item in the Content Tree. A developer simply has to format the string value correctly and use the item ID to get the item from the database before continuing with appropriate validation rule logic. However, it is important to be aware that the form parameters will vary based on how the validation rule is being accessed.

Generally, a validation rule being accessed through the Quick Action Bar or Validator Bar within the Content Editor will return a form parameters value with LoadItem and ID. For a Validate Button that accesses the rule from a modal the form parameters value likely equals “contenteditor:showvalidationresult” instead. In such a case, the focus would shift to the __CurrentItem key.

 

 var currentItem = httpCurrent.Request.Form["__CurrentItem"];

Form Current Item from within Field Validation Rule

 

The currentItem in this example would have a raw value similar to “sitecore://master/{CA5A53B9-518D-45D8-AE80-8D8BAD446118}?lang=en&ver=12”. In this instance, a developer would proceed by simply getting the item ID substring.  No additional formatting would be necessary before retrieving the item from the database and you should be able to continue with the validation logic.

As demonstrated above, it is possible for a developer to gain access to the currently loaded item fairly easily. Other item field values or a different item in relation to the current item within the Content tree may then be retrieved.

Did you find this helpful? If so, check out another one of my posts: Cross-Field Validation in Web Forms for Marketers

Michelle Banzer

Developer
Tags
  • .NET
  • Best Practices
  • Content Management
  • Sitecore
  • Tutorial
  • Web Development

Recent Work

Check out what else we've been working on