diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EmailFieldDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EmailFieldDriver.cs new file mode 100644 index 000000000..5260ccc78 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EmailFieldDriver.cs @@ -0,0 +1,57 @@ +using Orchard.DynamicForms.Elements; +using Orchard.Forms.Services; +using Orchard.Layouts.Framework.Drivers; +using DescribeContext = Orchard.Forms.Services.DescribeContext; + +namespace Orchard.DynamicForms.Drivers { + public class EmailFieldDriver : FormsElementDriver{ + public EmailFieldDriver(IFormManager formManager) : base(formManager) {} + + protected override EditorResult OnBuildEditor(EmailField element, ElementEditorContext context) { + var autoLabelEditor = BuildForm(context, "AutoLabel"); + var emailFieldValidation = BuildForm(context, "EmailFieldValidation", "Validation:10"); + + return Editor(context, autoLabelEditor, emailFieldValidation); + } + + protected override void DescribeForm(DescribeContext context) { + context.Form("EmailFieldValidation", factory => { + var shape = (dynamic)factory; + var form = shape.Fieldset( + Id: "EmailFieldValidation", + _IsRequired: shape.Checkbox( + Id: "IsRequired", + Name: "IsRequired", + Title: "Required", + Value: "true", + Description: T("Tick this checkbox to make this email field a required field.")), + _MaximumLength: shape.Textbox( + Id: "MaximumLength", + Name: "MaximumLength", + Title: "Maximum Length", + Classes: new[] { "text", "medium", "tokenized" }, + Description: T("The maximum length allowed.")), + _CompareWith: shape.Textbox( + Id: "CompareWith", + Name: "CompareWith", + Title: "Compare With", + Classes: new[] { "text", "medium", "tokenized" }, + Description: T("The name of another field whose value must match with this email field.")), + _CustomValidationMessage: shape.Textbox( + Id: "CustomValidationMessage", + Name: "CustomValidationMessage", + Title: "Custom Validation Message", + Classes: new[] { "text", "large", "tokenized" }, + Description: T("Optionally provide a custom validation message.")), + _ShowValidationMessage: shape.Checkbox( + Id: "ShowValidationMessage", + Name: "ShowValidationMessage", + Title: "Show Validation Message", + Value: "true", + Description: T("Autogenerate a validation message when a validation error occurs for the current field. Alternatively, to control the placement of the validation message you can use the ValidationMessage element instead."))); + + return form; + }); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/EmailField.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/EmailField.cs new file mode 100644 index 000000000..c6c9661f8 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/EmailField.cs @@ -0,0 +1,9 @@ +using Orchard.DynamicForms.Validators.Settings; + +namespace Orchard.DynamicForms.Elements { + public class EmailField : LabeledFormElement { + public EmailFieldValidationSettings ValidationSettings { + get { return State.GetModel(""); } + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Orchard.DynamicForms.csproj b/src/Orchard.Web/Modules/Orchard.DynamicForms/Orchard.DynamicForms.csproj index 8a00b244a..f71148971 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Orchard.DynamicForms.csproj +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Orchard.DynamicForms.csproj @@ -141,10 +141,12 @@ + + @@ -166,12 +168,15 @@ + + + @@ -384,6 +389,12 @@ + + + + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/ValidationRules/EmailAddress.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/ValidationRules/EmailAddress.cs new file mode 100644 index 000000000..8a4fdc542 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/ValidationRules/EmailAddress.cs @@ -0,0 +1,33 @@ +using System.Text.RegularExpressions; +using Orchard.DynamicForms.Helpers; +using Orchard.DynamicForms.Services; +using Orchard.DynamicForms.Services.Models; +using Orchard.Localization; + +namespace Orchard.DynamicForms.ValidationRules { + public class EmailAddress : ValidationRule { + public EmailAddress() { + RegexOptions = RegexOptions.Singleline | RegexOptions.IgnoreCase; + Pattern = @"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"; + } + + public string Pattern { get; set; } + public RegexOptions RegexOptions { get; set; } + + public override void Validate(ValidateInputContext context) { + if (!Regex.IsMatch(context.AttemptedValue, Pattern, RegexOptions)) { + var message = GetValidationMessage(context); + context.ModelState.AddModelError(context.FieldName, message.Text); + } + } + + public override void RegisterClientAttributes(RegisterClientValidationAttributesContext context) { + context.ClientAttributes["data-val-regex"] = GetValidationMessage(context).Text; + context.ClientAttributes["data-val-regex-pattern"] = Pattern; + } + + private LocalizedString GetValidationMessage(ValidationContext context) { + return T(ErrorMessage.WithDefault("{0} is not a valid email address."), context.FieldName, Pattern); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Validators/EmailFieldValidator.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Validators/EmailFieldValidator.cs new file mode 100644 index 000000000..856ddd6dd --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Validators/EmailFieldValidator.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using Orchard.DynamicForms.Elements; +using Orchard.DynamicForms.Services; +using Orchard.DynamicForms.ValidationRules; + +namespace Orchard.DynamicForms.Validators { + public class EmailFieldValidator : ElementValidator { + private readonly IValidationRuleFactory _validationRuleFactory; + public EmailFieldValidator(IValidationRuleFactory validationRuleFactory) { + _validationRuleFactory = validationRuleFactory; + } + + protected override IEnumerable GetValidationRules(EmailField element) { + var settings = element.ValidationSettings; + + if (settings.IsRequired == true) + yield return _validationRuleFactory.Create(settings.CustomValidationMessage); + + if (settings.MaximumLength != null) { + yield return _validationRuleFactory.Create(r => { + r.Maximum = settings.MaximumLength; + r.ErrorMessage = settings.CustomValidationMessage; + }); + } + + yield return _validationRuleFactory.Create(settings.CustomValidationMessage); + + if (!String.IsNullOrWhiteSpace(settings.CompareWith)) { + yield return _validationRuleFactory.Create(r => { + r.TargetName = settings.CompareWith; + r.ErrorMessage = settings.CustomValidationMessage; + }); + } + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Validators/Settings/EmailFieldValidationSettings.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Validators/Settings/EmailFieldValidationSettings.cs new file mode 100644 index 000000000..ab0fa882c --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Validators/Settings/EmailFieldValidationSettings.cs @@ -0,0 +1,9 @@ +using Orchard.DynamicForms.Services.Models; + +namespace Orchard.DynamicForms.Validators.Settings { + public class EmailFieldValidationSettings : ValidationSettingsBase { + public bool? IsRequired { get; set; } + public int? MaximumLength { get; set; } + public string CompareWith { get; set; } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Element-Form-EmailField.Design.cshtml b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Element-Form-EmailField.Design.cshtml new file mode 100644 index 000000000..bb08533e2 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Element-Form-EmailField.Design.cshtml @@ -0,0 +1,24 @@ +@using Orchard.DisplayManagement.Shapes +@using Orchard.DynamicForms.Elements +@using Orchard.Layouts.Helpers +@using Orchard.Layouts.Settings +@{ + var element = (EmailField)Model.Element; + var commonSettings = element.State.GetModel(); + var tagBuilder = (OrchardTagBuilder)TagBuilderExtensions.AddCommonElementAttributes(new OrchardTagBuilder("input"), Model); + + tagBuilder.AddCssClass("text design"); + tagBuilder.Attributes["type"] = "email"; + tagBuilder.Attributes["value"] = element.Value; + tagBuilder.Attributes["name"] = element.Name; +} + +@if (element.ShowLabel) { +
+ + @Html.Raw(tagBuilder.ToString(TagRenderMode.SelfClosing)) +
+} +else { + @Html.Raw(tagBuilder.ToString(TagRenderMode.SelfClosing)) +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Element-Form-EmailField.cshtml b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Element-Form-EmailField.cshtml new file mode 100644 index 000000000..9dbe36166 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Views/Element-Form-EmailField.cshtml @@ -0,0 +1,26 @@ +@using Orchard.DisplayManagement.Shapes +@using Orchard.DynamicForms.Elements +@using Orchard.Layouts.Helpers +@using Orchard.Layouts.Settings +@{ + var element = (EmailField)Model.Element; + var commonSettings = element.State.GetModel(); + var tagBuilder = (OrchardTagBuilder)TagBuilderExtensions.AddCommonElementAttributes(new OrchardTagBuilder("input"), Model); + + tagBuilder.AddCssClass("text"); + tagBuilder.Attributes["type"] = "email"; + tagBuilder.Attributes["name"] = element.Name; + tagBuilder.AddClientValidationAttributes((IDictionary)Model.ClientValidationAttributes); + + if (!ViewData.ModelState.IsValidField(element.Name)) { + tagBuilder.AddCssClass("input-validation-error"); + } +} + +@if (element.ShowLabel) { + +} +@Html.Raw(tagBuilder.ToString(TagRenderMode.SelfClosing)) +@if (element.ValidationSettings.ShowValidationMessage == true) { + @Html.ValidationMessage(element.Name) +} \ No newline at end of file