This commit is contained in:
jtkech
2016-03-25 03:42:33 +01:00
parent 336cda8c34
commit c1107b37bd
5 changed files with 54 additions and 32 deletions

View File

@@ -67,6 +67,7 @@ Scenario: Creating and using Boolean fields
And I fill in
| name | value |
| Fields[0].BooleanFieldSettings.Optional | false |
| Fields[0].BooleanFieldSettings.DefaultValue | Neutral |
And I fill in
| name | value |
| Fields[0].BooleanFieldSettings.NotSetLabel | May be |

View File

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

View File

@@ -48,9 +48,14 @@ namespace Orchard.Fields.Drivers {
if (updater.TryUpdateModel(field, GetPrefix(field, part), null, null)) {
var settings = field.PartFieldDefinition.Settings.GetModel<BooleanFieldSettings>();
if (!settings.Optional && !field.Value.HasValue) {
if (settings.DefaultValue.HasValue) {
field.Value = settings.DefaultValue;
}
else {
updater.AddModelError(field.Name, T("The field {0} is mandatory.", T(field.DisplayName)));
}
}
}
return ContentShape("Fields_Boolean_Edit", GetDifferentiator(field, part),
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: field, Prefix: GetPrefix(field, part)));

View File

@@ -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<EnumerationField> {
@@ -40,7 +41,7 @@ namespace Orchard.Fields.Drivers {
() => {
if (field.Value == null) {
var settings = field.PartFieldDefinition.Settings.GetModel<EnumerationFieldSettings>();
if (!String.IsNullOrEmpty(settings.DefaultValue)) {
if (!String.IsNullOrWhiteSpace(settings.DefaultValue)) {
field.Value = _tokenizer.Replace(settings.DefaultValue, new Dictionary<string, object> { { "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<EnumerationFieldSettings>();
if (field.SelectedValues.Length == 0 && !String.IsNullOrWhiteSpace(settings.DefaultValue) && !String.IsNullOrWhiteSpace(settings.Options)) {
field.Value = _tokenizer.Replace(settings.DefaultValue, new Dictionary<string, object> { { "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)));
}

View File

@@ -3,28 +3,27 @@
@{
var settings = Model.PartFieldDefinition.Settings.GetModel<EnumerationFieldSettings>();
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);
}
<fieldset>
<label for="@Html.FieldIdFor(m => m.Value)" @if (settings.Required) { <text> class="required" </text> }>@Model.DisplayName</label>
@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)) {
<label>@Html.RadioButton("Value", "", string.IsNullOrWhiteSpace(Model.Value), settings.Required ? new {required = "required"} : null)<i>@T("unset")</i></label>
}
<label>@Html.RadioButton("Value", "", string.IsNullOrWhiteSpace(Model.Value), isRequired ? new { required = "required" } : null)<i>@T("unset")</i></label> }
else {
<label>@Html.RadioButton("Value", option, (option == Model.Value), settings.Required ? new {required = "required"} : null)@option</label>
}
<label>@Html.RadioButton("Value", option, (option == Model.Value), isRequired ? new { required = "required" } : null)@option</label> }
}
break;
case ListMode.Listbox:
<input name="@Html.FieldNameFor(m => m.SelectedValues)" type="hidden" />
@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:
@@ -34,7 +33,7 @@
index++;
if (!string.IsNullOrWhiteSpace(option)) {
<div>
<input type="checkbox" name="@Html.FieldNameFor(m => m.SelectedValues)" value="@option" @((Model.SelectedValues != null && Model.SelectedValues.Contains(option)) ? "checked=\"checked\"" : "") class="check-box" id="@Html.FieldIdFor(m => m.SelectedValues)-@index" @if(settings.Required) {<text> required="required"</text> } />
<input type="checkbox" name="@Html.FieldNameFor(m => m.SelectedValues)" value="@option" @((Model.SelectedValues != null && Model.SelectedValues.Contains(option)) ? "checked=\"checked\"" : "") class="check-box" id="@Html.FieldIdFor(m => m.SelectedValues)-@index" />
<label class="forcheckbox" for="@Html.FieldIdFor(m => m.SelectedValues)-@index">@T(option)</label>
</div>
}
@@ -46,4 +45,7 @@
@if (HasText(settings.Hint)) {
<span class="hint">@settings.Hint</span>
}
@if (!String.IsNullOrWhiteSpace(settings.DefaultValue)) {
<span class="hint">@T("If no option is selected then the default value will be used.")</span>
}
</fieldset>