mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-21 03:14:10 +08:00
Squash
This commit is contained in:
@@ -65,8 +65,9 @@ Scenario: Creating and using Boolean fields
|
|||||||
# The value should be required
|
# The value should be required
|
||||||
When I go to "Admin/ContentTypes/Edit/Event"
|
When I go to "Admin/ContentTypes/Edit/Event"
|
||||||
And I fill in
|
And I fill in
|
||||||
| name | value |
|
| name | value |
|
||||||
| Fields[0].BooleanFieldSettings.Optional | false |
|
| Fields[0].BooleanFieldSettings.Optional | false |
|
||||||
|
| Fields[0].BooleanFieldSettings.DefaultValue | Neutral |
|
||||||
And I fill in
|
And I fill in
|
||||||
| name | value |
|
| name | value |
|
||||||
| Fields[0].BooleanFieldSettings.NotSetLabel | May be |
|
| Fields[0].BooleanFieldSettings.NotSetLabel | May be |
|
||||||
|
17
src/Orchard.Specs/Boolean.feature.cs
generated
17
src/Orchard.Specs/Boolean.feature.cs
generated
@@ -193,6 +193,9 @@ this.ScenarioSetup(scenarioInfo);
|
|||||||
table6.AddRow(new string[] {
|
table6.AddRow(new string[] {
|
||||||
"Fields[0].BooleanFieldSettings.Optional",
|
"Fields[0].BooleanFieldSettings.Optional",
|
||||||
"false"});
|
"false"});
|
||||||
|
table6.AddRow(new string[] {
|
||||||
|
"Fields[0].BooleanFieldSettings.DefaultValue",
|
||||||
|
"Neutral"});
|
||||||
#line 67
|
#line 67
|
||||||
testRunner.And("I fill in", ((string)(null)), table6, "And ");
|
testRunner.And("I fill in", ((string)(null)), table6, "And ");
|
||||||
#line hidden
|
#line hidden
|
||||||
@@ -202,7 +205,7 @@ this.ScenarioSetup(scenarioInfo);
|
|||||||
table7.AddRow(new string[] {
|
table7.AddRow(new string[] {
|
||||||
"Fields[0].BooleanFieldSettings.NotSetLabel",
|
"Fields[0].BooleanFieldSettings.NotSetLabel",
|
||||||
"May be"});
|
"May be"});
|
||||||
#line 70
|
#line 71
|
||||||
testRunner.And("I fill in", ((string)(null)), table7, "And ");
|
testRunner.And("I fill in", ((string)(null)), table7, "And ");
|
||||||
#line hidden
|
#line hidden
|
||||||
TechTalk.SpecFlow.Table table8 = new TechTalk.SpecFlow.Table(new string[] {
|
TechTalk.SpecFlow.Table table8 = new TechTalk.SpecFlow.Table(new string[] {
|
||||||
@@ -211,11 +214,11 @@ this.ScenarioSetup(scenarioInfo);
|
|||||||
table8.AddRow(new string[] {
|
table8.AddRow(new string[] {
|
||||||
"Fields[0].BooleanFieldSettings.SelectionMode",
|
"Fields[0].BooleanFieldSettings.SelectionMode",
|
||||||
"Radiobutton"});
|
"Radiobutton"});
|
||||||
#line 73
|
#line 74
|
||||||
testRunner.And("I fill in", ((string)(null)), table8, "And ");
|
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
|
#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 ");
|
testRunner.And("I go to \"Admin/Contents/Create/Event\"", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
|
||||||
#line hidden
|
#line hidden
|
||||||
TechTalk.SpecFlow.Table table9 = new TechTalk.SpecFlow.Table(new string[] {
|
TechTalk.SpecFlow.Table table9 = new TechTalk.SpecFlow.Table(new string[] {
|
||||||
@@ -224,11 +227,11 @@ this.ScenarioSetup(scenarioInfo);
|
|||||||
table9.AddRow(new string[] {
|
table9.AddRow(new string[] {
|
||||||
"Event.Active.Value",
|
"Event.Active.Value",
|
||||||
""});
|
""});
|
||||||
#line 78
|
#line 79
|
||||||
testRunner.And("I fill in", ((string)(null)), table9, "And ");
|
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
|
#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 ");
|
testRunner.Then("I should see \"The field Active is mandatory.\"", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then ");
|
||||||
#line hidden
|
#line hidden
|
||||||
this.ScenarioCleanup();
|
this.ScenarioCleanup();
|
||||||
|
@@ -48,7 +48,12 @@ namespace Orchard.Fields.Drivers {
|
|||||||
if (updater.TryUpdateModel(field, GetPrefix(field, part), null, null)) {
|
if (updater.TryUpdateModel(field, GetPrefix(field, part), null, null)) {
|
||||||
var settings = field.PartFieldDefinition.Settings.GetModel<BooleanFieldSettings>();
|
var settings = field.PartFieldDefinition.Settings.GetModel<BooleanFieldSettings>();
|
||||||
if (!settings.Optional && !field.Value.HasValue) {
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -7,6 +7,7 @@ using Orchard.Localization;
|
|||||||
using Orchard.Tokens;
|
using Orchard.Tokens;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Orchard.Fields.Drivers {
|
namespace Orchard.Fields.Drivers {
|
||||||
public class EnumerationFieldDriver : ContentFieldDriver<EnumerationField> {
|
public class EnumerationFieldDriver : ContentFieldDriver<EnumerationField> {
|
||||||
@@ -40,7 +41,7 @@ namespace Orchard.Fields.Drivers {
|
|||||||
() => {
|
() => {
|
||||||
if (field.Value == null) {
|
if (field.Value == null) {
|
||||||
var settings = field.PartFieldDefinition.Settings.GetModel<EnumerationFieldSettings>();
|
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 } });
|
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) {
|
protected override DriverResult Editor(ContentPart part, EnumerationField field, IUpdateModel updater, dynamic shapeHelper) {
|
||||||
if (updater.TryUpdateModel(field, GetPrefix(field, part), null, null)) {
|
if (updater.TryUpdateModel(field, GetPrefix(field, part), null, null)) {
|
||||||
var settings = field.PartFieldDefinition.Settings.GetModel<EnumerationFieldSettings>();
|
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) {
|
if (settings.Required && field.SelectedValues.Length == 0) {
|
||||||
updater.AddModelError(field.Name, T("The field {0} is mandatory", T(field.DisplayName)));
|
updater.AddModelError(field.Name, T("The field {0} is mandatory", T(field.DisplayName)));
|
||||||
}
|
}
|
||||||
|
@@ -3,47 +3,49 @@
|
|||||||
@{
|
@{
|
||||||
var settings = Model.PartFieldDefinition.Settings.GetModel<EnumerationFieldSettings>();
|
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() };
|
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>
|
<fieldset>
|
||||||
<label for="@Html.FieldIdFor(m => m.Value)" @if(settings.Required) { <text>class="required"</text> }>@Model.DisplayName</label>
|
<label for="@Html.FieldIdFor(m => m.Value)" @if (settings.Required) { <text> class="required" </text> }>@Model.DisplayName</label>
|
||||||
@switch (settings.ListMode) {
|
@switch (settings.ListMode) {
|
||||||
case ListMode.Dropdown:
|
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;
|
break;
|
||||||
|
|
||||||
case ListMode.Radiobutton:
|
case ListMode.Radiobutton:
|
||||||
foreach (var option in options) {
|
foreach (var option in options) {
|
||||||
if (string.IsNullOrWhiteSpace(option)) {
|
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 {
|
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;
|
break;
|
||||||
|
|
||||||
case ListMode.Listbox:
|
case ListMode.Listbox:
|
||||||
<input name="@Html.FieldNameFor(m => m.SelectedValues)" type="hidden" />
|
<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;
|
break;
|
||||||
|
|
||||||
case ListMode.Checkbox:
|
case ListMode.Checkbox:
|
||||||
int index = 0;
|
int index = 0;
|
||||||
<input name="@Html.FieldNameFor(m => m.SelectedValues)" type="hidden" />
|
<input name="@Html.FieldNameFor(m => m.SelectedValues)" type="hidden" />
|
||||||
foreach (var option in options) {
|
foreach (var option in options) {
|
||||||
index++;
|
index++;
|
||||||
if (!string.IsNullOrWhiteSpace(option)) {
|
if (!string.IsNullOrWhiteSpace(option)) {
|
||||||
<div>
|
<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>
|
<label class="forcheckbox" for="@Html.FieldIdFor(m => m.SelectedValues)-@index">@T(option)</label>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Html.ValidationMessageFor(m => m.SelectedValues)
|
@Html.ValidationMessageFor(m => m.SelectedValues)
|
||||||
@if (HasText(settings.Hint)) {
|
@if (HasText(settings.Hint)) {
|
||||||
<span class="hint">@settings.Hint</span>
|
<span class="hint">@settings.Hint</span>
|
||||||
}
|
}
|
||||||
</fieldset>
|
@if (!String.IsNullOrWhiteSpace(settings.DefaultValue)) {
|
||||||
|
<span class="hint">@T("If no option is selected then the default value will be used.")</span>
|
||||||
|
}
|
||||||
|
</fieldset>
|
Reference in New Issue
Block a user