mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-24 05:23:33 +08:00
Validating the comments form
Work Items: 16666, 16568 --HG-- branch : dev
This commit is contained in:
@@ -57,28 +57,5 @@ namespace Orchard.Tests.Utility.Extensions {
|
|||||||
var def = new LocalizedString("test");
|
var def = new LocalizedString("test");
|
||||||
Assert.That("bar".OrDefault(def).Text, Is.SameAs("bar"));
|
Assert.That("bar".OrDefault(def).Text, Is.SameAs("bar"));
|
||||||
}
|
}
|
||||||
[Test]
|
|
||||||
public void IsNullOrEmptyTrimmed_EmptyStringReturnsTrue() {
|
|
||||||
const string testString = "";
|
|
||||||
Assert.AreEqual(true, testString.IsNullOrEmptyTrimmed());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void IsNullOrEmptyTrimmed_NullStringReturnsTrue() {
|
|
||||||
const string testString = null;
|
|
||||||
Assert.AreEqual(true, testString.IsNullOrEmptyTrimmed());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void IsNullOrEmptyTrimmed_SpacedStringReturnsTrue() {
|
|
||||||
const string testString = " ";
|
|
||||||
Assert.AreEqual(true, testString.IsNullOrEmptyTrimmed());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void IsNullOrEmptyTrimmed_ActualStringReturnsFalse() {
|
|
||||||
const string testString = "testString";
|
|
||||||
Assert.AreEqual(false, testString.IsNullOrEmptyTrimmed());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using Orchard.Comments.Models;
|
using Orchard.Comments.Models;
|
||||||
using Orchard.Comments.Services;
|
using Orchard.Comments.Services;
|
||||||
@@ -7,6 +8,7 @@ using Orchard.ContentManagement;
|
|||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.UI.Notify;
|
using Orchard.UI.Notify;
|
||||||
using Orchard.Utility.Extensions;
|
using Orchard.Utility.Extensions;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Orchard.Comments.Controllers {
|
namespace Orchard.Comments.Controllers {
|
||||||
public class CommentController : Controller {
|
public class CommentController : Controller {
|
||||||
@@ -31,17 +33,8 @@ namespace Orchard.Comments.Controllers {
|
|||||||
: Redirect("~/");
|
: Redirect("~/");
|
||||||
|
|
||||||
var viewModel = new CommentsCreateViewModel();
|
var viewModel = new CommentsCreateViewModel();
|
||||||
try {
|
|
||||||
|
|
||||||
// UpdateModel(viewModel);
|
|
||||||
|
|
||||||
if(!TryUpdateModel(viewModel)) {
|
|
||||||
if (Request.Form["Name"].IsNullOrEmptyTrimmed()) {
|
|
||||||
_notifier.Error(T("You must provide a Name in order to comment"));
|
|
||||||
}
|
|
||||||
return Redirect(returnUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (TryUpdateModel(viewModel)) {
|
||||||
var context = new CreateCommentContext {
|
var context = new CreateCommentContext {
|
||||||
Author = viewModel.Name,
|
Author = viewModel.Name,
|
||||||
CommentText = viewModel.CommentText,
|
CommentText = viewModel.CommentText,
|
||||||
@@ -50,20 +43,25 @@ namespace Orchard.Comments.Controllers {
|
|||||||
CommentedOn = viewModel.CommentedOn
|
CommentedOn = viewModel.CommentedOn
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (!context.SiteName.StartsWith("http://") && !context.SiteName.StartsWith("https://")) {
|
||||||
|
context.SiteName = "http://" + context.SiteName;
|
||||||
|
}
|
||||||
|
|
||||||
CommentPart commentPart = _commentService.CreateComment(context, Services.WorkContext.CurrentSite.As<CommentSettingsPart>().Record.ModerateComments);
|
CommentPart commentPart = _commentService.CreateComment(context, Services.WorkContext.CurrentSite.As<CommentSettingsPart>().Record.ModerateComments);
|
||||||
|
|
||||||
if (commentPart.Record.Status == CommentStatus.Pending)
|
if (commentPart.Record.Status == CommentStatus.Pending)
|
||||||
Services.Notifier.Information(T("Your comment will appear after the site administrator approves it."));
|
Services.Notifier.Information(T("Your comment will appear after the site administrator approves it."));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
foreach (var error in ModelState.Values.SelectMany(m => m.Errors).Select( e=> e.ErrorMessage)) {
|
||||||
|
_notifier.Error(T(error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return !String.IsNullOrEmpty(returnUrl)
|
return !String.IsNullOrEmpty(returnUrl)
|
||||||
? Redirect(returnUrl)
|
? Redirect(returnUrl)
|
||||||
: Redirect("~/");
|
: Redirect("~/");
|
||||||
}
|
|
||||||
catch (Exception exception) {
|
|
||||||
_notifier.Error(T("Creating Comment failed: " + exception.Message));
|
|
||||||
// return View(viewModel);
|
|
||||||
return Redirect(returnUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -2,11 +2,21 @@
|
|||||||
|
|
||||||
namespace Orchard.Comments.ViewModels {
|
namespace Orchard.Comments.ViewModels {
|
||||||
public class CommentsCreateViewModel {
|
public class CommentsCreateViewModel {
|
||||||
[Required]
|
[Required(ErrorMessage="You must provide a Name in order to comment")]
|
||||||
|
[StringLength(255)]
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[RegularExpression(@"^[\w-]+@([\w-]+\.)+[\w]{2,4}$", ErrorMessage = "The Email is not valid")]
|
||||||
|
[StringLength(255)]
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
|
|
||||||
|
[StringLength(245)]
|
||||||
|
[RegularExpression(@"^(http(s)?://)?([\w-]+\.)+[\S]+$", ErrorMessage = "The Url is not valid")]
|
||||||
public string SiteName { get; set; }
|
public string SiteName { get; set; }
|
||||||
|
|
||||||
|
[Required(ErrorMessage = "You must provide a Comment")]
|
||||||
public string CommentText { get; set; }
|
public string CommentText { get; set; }
|
||||||
|
|
||||||
public int CommentedOn { get; set; }
|
public int CommentedOn { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -149,7 +149,7 @@ namespace Orchard.Media.Controllers {
|
|||||||
if (!Services.Authorizer.Authorize(Permissions.UploadMediaFiles, T("Couldn't upload media file")))
|
if (!Services.Authorizer.Authorize(Permissions.UploadMediaFiles, T("Couldn't upload media file")))
|
||||||
return new HttpUnauthorizedResult();
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
if(Request.Files[0].FileName.IsNullOrEmptyTrimmed()) {
|
if(String.IsNullOrWhiteSpace(Request.Files[0].FileName)) {
|
||||||
ModelState.AddModelError("File", T("Select a file to upload").ToString());
|
ModelState.AddModelError("File", T("Select a file to upload").ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -35,11 +35,6 @@ namespace Orchard.Utility.Extensions {
|
|||||||
return Regex.Replace(friendlier, @"[^a-zA-Z]+", m => m.Index == 0 ? "" : "-").ToLowerInvariant();
|
return Regex.Replace(friendlier, @"[^a-zA-Z]+", m => m.Index == 0 ? "" : "-").ToLowerInvariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsNullOrEmptyTrimmed(this string text) {
|
|
||||||
return text == null
|
|
||||||
|| string.IsNullOrEmpty(text.Trim());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static LocalizedString OrDefault(this string text, LocalizedString defaultValue) {
|
public static LocalizedString OrDefault(this string text, LocalizedString defaultValue) {
|
||||||
return string.IsNullOrEmpty(text)
|
return string.IsNullOrEmpty(text)
|
||||||
? defaultValue
|
? defaultValue
|
||||||
|
Reference in New Issue
Block a user