diff --git a/src/Orchard.Specs/Boolean.feature b/src/Orchard.Specs/Boolean.feature index c36d964ba..f85dd35f6 100644 --- a/src/Orchard.Specs/Boolean.feature +++ b/src/Orchard.Specs/Boolean.feature @@ -65,8 +65,9 @@ Scenario: Creating and using Boolean fields # The value should be required When I go to "Admin/ContentTypes/Edit/Event" And I fill in - | name | value | - | Fields[0].BooleanFieldSettings.Optional | false | + | name | value | + | Fields[0].BooleanFieldSettings.Optional | false | + | Fields[0].BooleanFieldSettings.DefaultValue | Neutral | And I fill in | name | value | | Fields[0].BooleanFieldSettings.NotSetLabel | May be | diff --git a/src/Orchard.Specs/Boolean.feature.cs b/src/Orchard.Specs/Boolean.feature.cs index f074e5cb2..f23a883d1 100644 --- a/src/Orchard.Specs/Boolean.feature.cs +++ b/src/Orchard.Specs/Boolean.feature.cs @@ -193,6 +193,9 @@ this.ScenarioSetup(scenarioInfo); table6.AddRow(new string[] { "Fields[0].BooleanFieldSettings.Optional", "false"}); + table6.AddRow(new string[] { + "Fields[0].BooleanFieldSettings.DefaultValue", + "Neutral"}); #line 67 testRunner.And("I fill in", ((string)(null)), table6, "And "); #line hidden @@ -202,7 +205,7 @@ this.ScenarioSetup(scenarioInfo); table7.AddRow(new string[] { "Fields[0].BooleanFieldSettings.NotSetLabel", "May be"}); -#line 70 +#line 71 testRunner.And("I fill in", ((string)(null)), table7, "And "); #line hidden TechTalk.SpecFlow.Table table8 = new TechTalk.SpecFlow.Table(new string[] { @@ -211,11 +214,11 @@ this.ScenarioSetup(scenarioInfo); table8.AddRow(new string[] { "Fields[0].BooleanFieldSettings.SelectionMode", "Radiobutton"}); -#line 73 +#line 74 testRunner.And("I fill in", ((string)(null)), table8, "And "); -#line 76 - testRunner.And("I hit \"Save\"", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And "); #line 77 + testRunner.And("I hit \"Save\"", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And "); +#line 78 testRunner.And("I go to \"Admin/Contents/Create/Event\"", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And "); #line hidden TechTalk.SpecFlow.Table table9 = new TechTalk.SpecFlow.Table(new string[] { @@ -224,11 +227,11 @@ this.ScenarioSetup(scenarioInfo); table9.AddRow(new string[] { "Event.Active.Value", ""}); -#line 78 +#line 79 testRunner.And("I fill in", ((string)(null)), table9, "And "); -#line 81 - testRunner.And("I hit \"Save\"", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And "); #line 82 + testRunner.And("I hit \"Save\"", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And "); +#line 83 testRunner.Then("I should see \"The field Active is mandatory.\"", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then "); #line hidden this.ScenarioCleanup(); diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/BooleanFieldDriver.cs b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/BooleanFieldDriver.cs index fbf3ca61c..683e712e9 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/BooleanFieldDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/BooleanFieldDriver.cs @@ -48,7 +48,12 @@ namespace Orchard.Fields.Drivers { if (updater.TryUpdateModel(field, GetPrefix(field, part), null, null)) { var settings = field.PartFieldDefinition.Settings.GetModel(); if (!settings.Optional && !field.Value.HasValue) { - updater.AddModelError(field.Name, T("The field {0} is mandatory.", T(field.DisplayName))); + if (settings.DefaultValue.HasValue) { + field.Value = settings.DefaultValue; + } + else { + updater.AddModelError(field.Name, T("The field {0} is mandatory.", T(field.DisplayName))); + } } } diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/EnumerationFieldDriver.cs b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/EnumerationFieldDriver.cs index 044844d5d..46082bf7a 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/EnumerationFieldDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/EnumerationFieldDriver.cs @@ -7,6 +7,7 @@ using Orchard.Localization; using Orchard.Tokens; using System; using System.Collections.Generic; +using System.Linq; namespace Orchard.Fields.Drivers { public class EnumerationFieldDriver : ContentFieldDriver { @@ -40,7 +41,7 @@ namespace Orchard.Fields.Drivers { () => { if (field.Value == null) { var settings = field.PartFieldDefinition.Settings.GetModel(); - if (!String.IsNullOrEmpty(settings.DefaultValue)) { + if (!String.IsNullOrWhiteSpace(settings.DefaultValue)) { field.Value = _tokenizer.Replace(settings.DefaultValue, new Dictionary { { "Content", part.ContentItem } }); } } @@ -52,6 +53,16 @@ namespace Orchard.Fields.Drivers { protected override DriverResult Editor(ContentPart part, EnumerationField field, IUpdateModel updater, dynamic shapeHelper) { if (updater.TryUpdateModel(field, GetPrefix(field, part), null, null)) { var settings = field.PartFieldDefinition.Settings.GetModel(); + + if (field.SelectedValues.Length == 0 && !String.IsNullOrWhiteSpace(settings.DefaultValue) && !String.IsNullOrWhiteSpace(settings.Options)) { + field.Value = _tokenizer.Replace(settings.DefaultValue, new Dictionary { { "Content", part.ContentItem } }); + + string[] options = settings.Options.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); + var selectedValues = field.SelectedValues.ToList(); + selectedValues.RemoveAll(value => !options.Any(value.Equals)); + field.SelectedValues = selectedValues.ToArray(); + } + if (settings.Required && field.SelectedValues.Length == 0) { updater.AddModelError(field.Name, T("The field {0} is mandatory", T(field.DisplayName))); } diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Enumeration.Edit.cshtml b/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Enumeration.Edit.cshtml index d3f4ebda4..8c89ec5ab 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Enumeration.Edit.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Enumeration.Edit.cshtml @@ -3,47 +3,49 @@ @{ var settings = Model.PartFieldDefinition.Settings.GetModel(); string[] options = (!String.IsNullOrWhiteSpace(settings.Options)) ? settings.Options.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None) : new string[] { T("Select an option").ToString() }; + var isRequired = settings.Required && String.IsNullOrWhiteSpace(settings.DefaultValue); }
- + @switch (settings.ListMode) { case ListMode.Dropdown: - @Html.DropDownListFor(m => m.Value, new SelectList(options, Model.Value), settings.Required ? new {required = "required"} : null) + @Html.DropDownListFor(m => m.Value, new SelectList(options, Model.Value), isRequired ? new { required = "required" } : null) break; - + case ListMode.Radiobutton: foreach (var option in options) { - if (string.IsNullOrWhiteSpace(option)) { - - } + if (string.IsNullOrWhiteSpace(option)) { + } else { - - } + } } break; - + case ListMode.Listbox: - @Html.ListBoxFor(m => m.SelectedValues, new MultiSelectList(options, Model.SelectedValues), settings.Required ? new {required = "required"} : null) + @Html.ListBoxFor(m => m.SelectedValues, new MultiSelectList(options, Model.SelectedValues), isRequired ? new { required = "required" } : null) break; case ListMode.Checkbox: int index = 0; - foreach (var option in options) { - index++; - if (!string.IsNullOrWhiteSpace(option)) { + foreach (var option in options) { + index++; + if (!string.IsNullOrWhiteSpace(option)) {
- required="required" } /> - + +
- } - } - break; + } + } + break; } - + @Html.ValidationMessageFor(m => m.SelectedValues) @if (HasText(settings.Hint)) { - @settings.Hint + @settings.Hint } -
+ @if (!String.IsNullOrWhiteSpace(settings.DefaultValue)) { + @T("If no option is selected then the default value will be used.") + } + \ No newline at end of file