#5437: Maintaining selected values after model validation error.

This commit is contained in:
Sipke Schoorstra
2015-06-27 18:31:08 +03:00
parent c435a98670
commit 2c496a36db
10 changed files with 63 additions and 7 deletions

View File

@@ -129,6 +129,7 @@ namespace Orchard.DynamicForms.Drivers {
private IEnumerable<SelectListItem> 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<string> GetRuntimeValues(Query element) {
var runtimeValue = element.RuntimeValue;
return runtimeValue != null ? runtimeValue.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) : Enumerable.Empty<string>();
}
}
}

View File

@@ -132,6 +132,7 @@ namespace Orchard.DynamicForms.Drivers {
private IEnumerable<SelectListItem> 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<string> GetRuntimeValues(Taxonomy element) {
var runtimeValue = element.RuntimeValue;
return runtimeValue != null ? runtimeValue.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) : Enumerable.Empty<string>();
}
}
}

View File

@@ -9,15 +9,21 @@ using Orchard.Layouts.Helpers;
namespace Orchard.DynamicForms.Elements {
public class Enumeration : LabeledFormElement {
private readonly Lazy<IEnumerable<SelectListItem>> _options;
private readonly Lazy<IEnumerable<string>> _runtimeValues;
public Enumeration() {
_options = new Lazy<IEnumerable<SelectListItem>>(GetOptions);
_runtimeValues = new Lazy<IEnumerable<string>>(ParseRuntimeValues);
}
public IEnumerable<SelectListItem> Options {
get { return _options.Value; }
}
public IEnumerable<string> 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<string> ParseRuntimeValues() {
var runtimeValue = RuntimeValue;
return runtimeValue != null ? runtimeValue.Split(new[] {',', ';'}, StringSplitOptions.RemoveEmptyEntries) : Enumerable.Empty<string>();
}
private IEnumerable<SelectListItem> 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)
};
}
}

View File

@@ -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); }

View File

@@ -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<string, string>)Model.ClientValidationAttributes);
}

View File

@@ -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<string, string>) Model.ClientValidationAttributes);
}

View File

@@ -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<string, string>)Model.ClientValidationAttributes);
}

View File

@@ -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<string, string>)Model.ClientValidationAttributes);
}

View File

@@ -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<string, string>)Model.ClientValidationAttributes);
}

View File

@@ -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<string, string>)Model.ClientValidationAttributes);
}