Last active
December 24, 2015 13:29
-
-
Save PetersonDave/6805162 to your computer and use it in GitHub Desktop.
Form field validation for complex fields. Parse query in field's parameters field value. Query then executed for query results, representing data source of a drop down list field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static bool IsValueInFieldDatasource(IEnumerable<ControlResult> fields, string fieldName, string value) | |
{ | |
Sitecore.Diagnostics.Assert.IsNotNullOrEmpty(fieldName, "fieldName cannot be null"); | |
if (string.IsNullOrEmpty(value)) return false; | |
bool isValidSelection = false; | |
var field = fields.FirstOrDefault(x => string.Compare(x.FieldName, fieldName, StringComparison.OrdinalIgnoreCase) == 0); | |
if (field != null) | |
{ | |
var parameters = Sitecore.Context.Database.Items[field.FieldID].Fields["Parameters"]; | |
if (parameters.HasValue) | |
{ | |
IEnumerable<Pair<string, string>> p = ParametersUtil.XmlToPairArray(parameters.Value, true); | |
var query = p.FirstOrDefault(); | |
bool isQueryValid = query != null && !string.IsNullOrEmpty(query.Part2); | |
if (isQueryValid) | |
{ | |
var querySettings = QuerySettings.ParseRange(query.Part2); | |
var dataSource = QueryManager.Select(querySettings); | |
isValidSelection = dataSource.Get(value) != null; | |
} | |
} | |
} | |
return isValidSelection; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment