Fixing email validation

--HG--
extra : source : c3cd097822f7cb9ea35ba68b38b87d0b050b74c2
This commit is contained in:
Sebastien Ros
2013-04-13 11:36:17 -07:00
parent 7f8a82c9f1
commit 20736a4ba3
2 changed files with 20 additions and 13 deletions

View File

@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Web.Mvc;
using Orchard.Comments.Models;
using Orchard.Comments.Services;
@@ -13,12 +12,10 @@ namespace Orchard.Comments.Controllers {
public class CommentController : Controller {
public IOrchardServices Services { get; set; }
private readonly ICommentService _commentService;
private readonly INotifier _notifier;
public CommentController(IOrchardServices services, ICommentService commentService, INotifier notifier) {
public CommentController(IOrchardServices services, ICommentService commentService) {
Services = services;
_commentService = commentService;
_notifier = notifier;
T = NullLocalizer.Instance;
}
@@ -32,6 +29,22 @@ namespace Orchard.Comments.Controllers {
var viewModel = new CommentsCreateViewModel();
TryUpdateModel(viewModel);
if (!ModelState.IsValidField("Name")) {
Services.Notifier.Error(T("Name is mandatory and must have less than 255 chars"));
}
if (!ModelState.IsValidField("Email")) {
Services.Notifier.Error(T("Email is invalid or is longer than 255 chars"));
}
if (!ModelState.IsValidField("Site")) {
Services.Notifier.Error(T("Site url is invalid or is longer than 255 chars"));
}
if (!ModelState.IsValidField("CommentText")) {
Services.Notifier.Error(T("Comment is mandatory"));
}
var context = new CreateCommentContext {
Author = viewModel.Name,
@@ -41,7 +54,6 @@ namespace Orchard.Comments.Controllers {
CommentedOn = viewModel.CommentedOn
};
if (ModelState.IsValid) {
if (!String.IsNullOrEmpty(context.SiteName) && !context.SiteName.StartsWith("http://") && !context.SiteName.StartsWith("https://")) {
context.SiteName = "http://" + context.SiteName;
@@ -60,12 +72,6 @@ namespace Orchard.Comments.Controllers {
}
}
else {
foreach (var error in ModelState.Values.SelectMany(m => m.Errors).Select( e=> e.ErrorMessage)) {
_notifier.Error(T(error));
}
}
if(!ModelState.IsValid) {
TempData["CreateCommentContext.Name"] = context.Author;
TempData["CreateCommentContext.CommentText"] = context.CommentText;
TempData["CreateCommentContext.Email"] = context.Email;

View File

@@ -3,17 +3,18 @@ using System.ComponentModel.DataAnnotations;
namespace Orchard.Comments.ViewModels {
public class CommentsCreateViewModel {
[Required]
[StringLength(255)]
public string Name { get; set; }
[RegularExpression(@"^[^@\s]+@[^@\s]+$", ErrorMessage = "Invalid Email")]
[RegularExpression(@"^(?![\.@])(""([^""\r\\]|\\[""\r\\])*""|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$")]
[StringLength(255)]
public string Email { get; set; }
[StringLength(245)]
[DisplayName("Site")]
[RegularExpression(@"^(http(s)?://)?([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}[\S]+$", ErrorMessage = "Invalid url")]
[RegularExpression(@"^(http(s)?://)?([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}[\S]+$")]
public string SiteName { get; set; }
[Required, DisplayName("Comment")]