mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Squash
This commit is contained in:
@@ -48,7 +48,12 @@ 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) {
|
||||
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 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)));
|
||||
}
|
||||
|
||||
@@ -3,47 +3,49 @@
|
||||
@{
|
||||
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>
|
||||
<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>
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(option)) {
|
||||
<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:
|
||||
int index = 0;
|
||||
<input name="@Html.FieldNameFor(m => m.SelectedValues)" type="hidden" />
|
||||
foreach (var option in options) {
|
||||
index++;
|
||||
if (!string.IsNullOrWhiteSpace(option)) {
|
||||
foreach (var option in options) {
|
||||
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> } />
|
||||
<label class="forcheckbox" for="@Html.FieldIdFor(m => m.SelectedValues)-@index">@T(option)</label>
|
||||
<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>
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@Html.ValidationMessageFor(m => m.SelectedValues)
|
||||
@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