Skip to content

Commit

Permalink
Fixing issue with Youtube field which is always required (#2534)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skrypt authored and sebastienros committed Oct 18, 2018
1 parent 426f951 commit d4367d6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
@@ -1,19 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.WebUtilities;
using OrchardCore.ContentFields.Fields;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentFields.Settings;
using OrchardCore.ContentFields.ViewModels;
using OrchardCore.ContentManagement.Display.ContentDisplay;
using OrchardCore.ContentManagement.Display.Models;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;

namespace OrchardCore.ContentFields.Fields
{
public class YoutubeFieldDisplayDriver : ContentFieldDisplayDriver<YoutubeField>
{
public YoutubeFieldDisplayDriver(IStringLocalizer<YoutubeFieldDisplayDriver> localizer)
{
T = localizer;
}

public IStringLocalizer T { get; set; }

public override IDisplayResult Display(YoutubeField field, BuildFieldDisplayContext context)
{
Expand Down Expand Up @@ -41,36 +47,40 @@ public override async Task<IDisplayResult> UpdateAsync(YoutubeField field, IUpda
{
EditYoutubeFieldViewModel model = new EditYoutubeFieldViewModel();

await updater.TryUpdateModelAsync(model, Prefix);

if (string.IsNullOrEmpty(model.RawAddress))
if (await updater.TryUpdateModelAsync(model, Prefix))
{
return Edit(field, context);
}

var uri = new Uri(model.RawAddress);

// if it is a url with QueryString
if (!string.IsNullOrWhiteSpace(uri.Query))
{
var query = QueryHelpers.ParseQuery(uri.Query);
if (query.ContainsKey("v"))
var settings = context.PartFieldDefinition.Settings.ToObject<YoutubeFieldSettings>();
if (settings.Required && String.IsNullOrWhiteSpace(model.RawAddress))
{
model.EmbeddedAddress = $"{uri.GetLeftPart(UriPartial.Authority)}/embed/{query["v"]}";
updater.ModelState.AddModelError(Prefix, T["A value is required for '{0}'.", context.PartFieldDefinition.DisplayName()]);
}
else
{
updater.ModelState.AddModelError(Prefix + "." + nameof(model.RawAddress), "The url format is not correct");
var uri = new Uri(model.RawAddress);

// if it is a url with QueryString
if (!String.IsNullOrWhiteSpace(uri.Query))
{
var query = QueryHelpers.ParseQuery(uri.Query);
if (query.ContainsKey("v"))
{
model.EmbeddedAddress = $"{uri.GetLeftPart(UriPartial.Authority)}/embed/{query["v"]}";
}
else
{
updater.ModelState.AddModelError(Prefix + "." + nameof(model.RawAddress), T["The format of the url is invalid"]);
}
}
else
{
var path = uri.AbsolutePath.Split('?')[0];
model.EmbeddedAddress = $"{uri.GetLeftPart(UriPartial.Authority)}/embed/{path}";
}

field.RawAddress = model.RawAddress;
field.EmbeddedAddress = model.EmbeddedAddress;
}
}
else
{
string path = uri.AbsolutePath.Split('?')[0];
model.EmbeddedAddress = $"{uri.GetLeftPart(UriPartial.Authority)}/embed/{path}";
}

field.RawAddress = model.RawAddress;
field.EmbeddedAddress = model.EmbeddedAddress;

return Edit(field, context);
}
Expand Down
Expand Up @@ -6,5 +6,6 @@ public class YoutubeFieldSettings
public string Label { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public bool Required { get; set; }
}
}
Expand Up @@ -10,7 +10,6 @@ namespace OrchardCore.ContentFields.ViewModels
{
public class EditYoutubeFieldViewModel : YoutubeFieldDisplayViewModel
{
[Required(ErrorMessage = "Youtube field is required")]
[DataType(DataType.Url, ErrorMessage = "The field only accepts Urls")]
public string RawAddress { get; set; }
public string EmbeddedAddress { get; set; }
Expand Down
Expand Up @@ -8,7 +8,7 @@

<fieldset class="form-group">
<label asp-for="RawAddress">@Model.PartFieldDefinition.DisplayName()</label>
<input asp-for="RawAddress" class="form-control content-preview-text" />
<input asp-for="RawAddress" class="form-control content-preview-text" required="@settings.Required" />
@if (!String.IsNullOrEmpty(settings.Hint))
{
<span class="hint">@settings.Hint</span>
Expand Down
Expand Up @@ -6,6 +6,12 @@
@{

}
<fieldset class="form-group">
<div class="custom-control custom-checkbox">
<input asp-for="Required" type="checkbox" class="custom-control-input">
<label class="custom-control-label" asp-for="Required">@T["Check if the field is required."]</label>
</div>
</fieldset>
<fieldset class="form-group">
<div class="row col-sm-6">
<label asp-for="Hint">@T["Hint"]</label>
Expand Down

0 comments on commit d4367d6

Please sign in to comment.