From 2c496a36db7aa8dd378c4f66bdb48eb344018b6e Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sat, 27 Jun 2015 18:31:08 +0300 Subject: [PATCH] #5437: Maintaining selected values after model validation error. --- .../Drivers/QueryElementDriver.cs | 10 ++++++++-- .../Drivers/TaxonomyElementDriver.cs | 9 ++++++++- .../Elements/Enumeration.cs | 19 ++++++++++++++++--- .../Orchard.DynamicForms/Elements/Query.cs | 2 +- .../Elements/Enumeration-CheckList.cshtml | 5 +++++ .../Elements/Enumeration-RadioList.cshtml | 5 +++++ .../Views/Elements/Query-CheckList.cshtml | 5 +++++ .../Views/Elements/Query-RadioList.cshtml | 5 +++++ .../Views/Elements/Taxonomy-CheckList.cshtml | 5 +++++ .../Views/Elements/Taxonomy-RadioList.cshtml | 5 +++++ 10 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/QueryElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/QueryElementDriver.cs index 65c7d3fa0..a2f6b144c 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/QueryElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/QueryElementDriver.cs @@ -129,6 +129,7 @@ namespace Orchard.DynamicForms.Drivers { private IEnumerable GetOptions(Query element, int? queryId) { var optionLabel = element.OptionLabel; + var runtimeValues = GetRuntimeValues(element); if (!String.IsNullOrWhiteSpace(optionLabel)) { yield return new SelectListItem { Text = optionLabel }; @@ -137,7 +138,6 @@ namespace Orchard.DynamicForms.Drivers { if (queryId == null) yield break; - var contentItems = _projectionManager.GetContentItems(queryId.Value).ToArray(); var valueExpression = !String.IsNullOrWhiteSpace(element.ValueExpression) ? element.ValueExpression : "{Content.Id}"; var textExpression = !String.IsNullOrWhiteSpace(element.TextExpression) ? element.TextExpression : "{Content.DisplayText}"; @@ -149,9 +149,15 @@ namespace Orchard.DynamicForms.Drivers { yield return new SelectListItem { Text = text, - Value = value + Value = value, + Selected = runtimeValues.Contains(value, StringComparer.OrdinalIgnoreCase) }; } } + + private IEnumerable GetRuntimeValues(Query element) { + var runtimeValue = element.RuntimeValue; + return runtimeValue != null ? runtimeValue.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) : Enumerable.Empty(); + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TaxonomyElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TaxonomyElementDriver.cs index 35aae07c7..5fa741f8d 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TaxonomyElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TaxonomyElementDriver.cs @@ -132,6 +132,7 @@ namespace Orchard.DynamicForms.Drivers { private IEnumerable GetTermOptions(Taxonomy element, int? taxonomyId) { var optionLabel = element.OptionLabel; + var runtimeValues = GetRuntimeValues(element); if (!String.IsNullOrWhiteSpace(optionLabel)) { yield return new SelectListItem { Text = optionLabel }; @@ -151,7 +152,8 @@ namespace Orchard.DynamicForms.Drivers { return new SelectListItem { Text = text, - Value = value + Value = value, + Selected = runtimeValues.Contains(value, StringComparer.OrdinalIgnoreCase) }; }); @@ -168,5 +170,10 @@ namespace Orchard.DynamicForms.Drivers { yield return item; } } + + private IEnumerable GetRuntimeValues(Taxonomy element) { + var runtimeValue = element.RuntimeValue; + return runtimeValue != null ? runtimeValue.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) : Enumerable.Empty(); + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Enumeration.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Enumeration.cs index e89da61aa..93c400b08 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Enumeration.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Enumeration.cs @@ -9,15 +9,21 @@ using Orchard.Layouts.Helpers; namespace Orchard.DynamicForms.Elements { public class Enumeration : LabeledFormElement { private readonly Lazy> _options; + private readonly Lazy> _runtimeValues; public Enumeration() { _options = new Lazy>(GetOptions); + _runtimeValues = new Lazy>(ParseRuntimeValues); } public IEnumerable Options { get { return _options.Value; } } + public IEnumerable RuntimeValues { + get { return _runtimeValues.Value; } + } + public string InputType { get { return this.Retrieve(x => x.InputType, () => "SelectList"); } set { this.Store(x => x.InputType, value); } @@ -31,13 +37,18 @@ namespace Orchard.DynamicForms.Elements { return ParseOptionsText(); } + private IEnumerable ParseRuntimeValues() { + var runtimeValue = RuntimeValue; + return runtimeValue != null ? runtimeValue.Split(new[] {',', ';'}, StringSplitOptions.RemoveEmptyEntries) : Enumerable.Empty(); + } + private IEnumerable ParseOptionsText() { var data = this.Retrieve("Options", () => ""); var lines = Regex.Split(data, @"(?:\r\n|[\r\n])", RegexOptions.Multiline); return lines.Select(ParseLine).Where(x => x != null); } - private static SelectListItem ParseLine(string line) { + private SelectListItem ParseLine(string line) { if (String.IsNullOrWhiteSpace(line)) return null; @@ -47,7 +58,8 @@ namespace Orchard.DynamicForms.Elements { var value = parts[0].Trim(); return new SelectListItem { Text = value, - Value = value + Value = value, + Selected = RuntimeValues.Contains(value, StringComparer.OrdinalIgnoreCase) }; } else { @@ -55,7 +67,8 @@ namespace Orchard.DynamicForms.Elements { var value = String.Join(":", parts.Skip(1)).Trim(); return new SelectListItem { Text = text, - Value = value + Value = value, + Selected = RuntimeValues.Contains(value, StringComparer.OrdinalIgnoreCase) }; } } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Query.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Query.cs index a40805cff..293c9e06a 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Query.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Query.cs @@ -3,7 +3,7 @@ using Orchard.Layouts.Helpers; namespace Orchard.DynamicForms.Elements { public class Query : LabeledFormElement { - + public string InputType { get { return this.Retrieve(x => x.InputType, () => "SelectList"); } set { this.Store(x => x.InputType, value); } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Enumeration-CheckList.cshtml b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Enumeration-CheckList.cshtml index 5b05d382b..7f37065b6 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Enumeration-CheckList.cshtml +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Enumeration-CheckList.cshtml @@ -15,6 +15,11 @@ inputTagBuilder.Attributes["type"] = "checkbox"; inputTagBuilder.Attributes["name"] = element.Name; inputTagBuilder.Attributes["value"] = option.Value; + + if (option.Selected) { + inputTagBuilder.Attributes["checked"] = "checked"; + } + if (element.ValidationSettings.Required == true && index == 0) { inputTagBuilder.AddClientValidationAttributes((IDictionary)Model.ClientValidationAttributes); } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Enumeration-RadioList.cshtml b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Enumeration-RadioList.cshtml index 985eea326..5f3d700d7 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Enumeration-RadioList.cshtml +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Enumeration-RadioList.cshtml @@ -15,6 +15,11 @@ inputTagBuilder.Attributes["type"] = "radio"; inputTagBuilder.Attributes["name"] = element.Name; inputTagBuilder.Attributes["value"] = option.Value; + + if (option.Selected) { + inputTagBuilder.Attributes["checked"] = "checked"; + } + if (element.ValidationSettings.Required == true && index == 0) { inputTagBuilder.AddClientValidationAttributes((IDictionary) Model.ClientValidationAttributes); } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Query-CheckList.cshtml b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Query-CheckList.cshtml index 8dacf7472..0df9fefd4 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Query-CheckList.cshtml +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Query-CheckList.cshtml @@ -15,6 +15,11 @@ inputTagBuilder.Attributes["type"] = "checkbox"; inputTagBuilder.Attributes["name"] = element.Name; inputTagBuilder.Attributes["value"] = option.Value; + + if (option.Selected) { + inputTagBuilder.Attributes["checked"] = "checked"; + } + if (element.ValidationSettings.Required == true && index == 0) { inputTagBuilder.AddClientValidationAttributes((IDictionary)Model.ClientValidationAttributes); } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Query-RadioList.cshtml b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Query-RadioList.cshtml index e99cef71b..0032b9778 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Query-RadioList.cshtml +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Query-RadioList.cshtml @@ -15,6 +15,11 @@ inputTagBuilder.Attributes["type"] = "radio"; inputTagBuilder.Attributes["name"] = element.Name; inputTagBuilder.Attributes["value"] = option.Value; + + if (option.Selected) { + inputTagBuilder.Attributes["checked"] = "checked"; + } + if (element.ValidationSettings.Required == true && index == 0) { inputTagBuilder.AddClientValidationAttributes((IDictionary)Model.ClientValidationAttributes); } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Taxonomy-CheckList.cshtml b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Taxonomy-CheckList.cshtml index c63d020a1..41f3c5e65 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Taxonomy-CheckList.cshtml +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Taxonomy-CheckList.cshtml @@ -15,6 +15,11 @@ inputTagBuilder.Attributes["type"] = "checkbox"; inputTagBuilder.Attributes["name"] = element.Name; inputTagBuilder.Attributes["value"] = option.Value; + + if (option.Selected) { + inputTagBuilder.Attributes["checked"] = "checked"; + } + if (element.ValidationSettings.Required == true && index == 0) { inputTagBuilder.AddClientValidationAttributes((IDictionary)Model.ClientValidationAttributes); } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Taxonomy-RadioList.cshtml b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Taxonomy-RadioList.cshtml index e17d46dfb..e65cf10f6 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Taxonomy-RadioList.cshtml +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Elements/Taxonomy-RadioList.cshtml @@ -15,6 +15,11 @@ inputTagBuilder.Attributes["type"] = "radio"; inputTagBuilder.Attributes["name"] = element.Name; inputTagBuilder.Attributes["value"] = option.Value; + + if (option.Selected) { + inputTagBuilder.Attributes["checked"] = "checked"; + } + if (element.ValidationSettings.Required == true && index == 0) { inputTagBuilder.AddClientValidationAttributes((IDictionary)Model.ClientValidationAttributes); }