mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Adding EmailField form element.
This commit is contained in:
@@ -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<EmailField>{
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Orchard.DynamicForms.Validators.Settings;
|
||||
|
||||
namespace Orchard.DynamicForms.Elements {
|
||||
public class EmailField : LabeledFormElement {
|
||||
public EmailFieldValidationSettings ValidationSettings {
|
||||
get { return State.GetModel<EmailFieldValidationSettings>(""); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,10 +141,12 @@
|
||||
<Compile Include="Controllers\SubmissionAdminController.cs" />
|
||||
<Compile Include="Controllers\AdminController.cs" />
|
||||
<Compile Include="Controllers\FormController.cs" />
|
||||
<Compile Include="Drivers\EmailFieldDriver.cs" />
|
||||
<Compile Include="Drivers\PasswordFieldDriver.cs" />
|
||||
<Compile Include="Drivers\QueryDriver.cs" />
|
||||
<Compile Include="Drivers\ValidationMessageDriver.cs" />
|
||||
<Compile Include="Drivers\ValidationSummaryDriver.cs" />
|
||||
<Compile Include="Elements\EmailField.cs" />
|
||||
<Compile Include="Elements\PasswordField.cs" />
|
||||
<Compile Include="Elements\Query.cs" />
|
||||
<Compile Include="Elements\ValidationMessage.cs" />
|
||||
@@ -166,12 +168,15 @@
|
||||
<Compile Include="Services\IValidationRule.cs" />
|
||||
<Compile Include="ValidationRules\Mandatory.cs" />
|
||||
<Compile Include="ValidationRules\Compare.cs" />
|
||||
<Compile Include="ValidationRules\EmailAddress.cs" />
|
||||
<Compile Include="ValidationRules\RegularExpression.cs" />
|
||||
<Compile Include="ValidationRules\StringLength.cs" />
|
||||
<Compile Include="Validators\CheckBoxValidator.cs" />
|
||||
<Compile Include="ValidationRules\Required.cs" />
|
||||
<Compile Include="Services\ValidationRule.cs" />
|
||||
<Compile Include="Validators\EmailFieldValidator.cs" />
|
||||
<Compile Include="Validators\Settings\CheckBoxValidationSettings.cs" />
|
||||
<Compile Include="Validators\Settings\EmailFieldValidationSettings.cs" />
|
||||
<Compile Include="Validators\Settings\PasswordFieldValidationSettings.cs" />
|
||||
<Compile Include="Validators\Settings\TextFieldValidationSettings.cs" />
|
||||
<Compile Include="Services\Models\ValidationSettingsBase.cs" />
|
||||
@@ -384,6 +389,12 @@
|
||||
<ItemGroup>
|
||||
<Content Include="Views\Element-Form-Query-RadioList.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Views\Element-Form-EmailField.Design.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Views\Element-Form-EmailField.cshtml" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<EmailField> {
|
||||
private readonly IValidationRuleFactory _validationRuleFactory;
|
||||
public EmailFieldValidator(IValidationRuleFactory validationRuleFactory) {
|
||||
_validationRuleFactory = validationRuleFactory;
|
||||
}
|
||||
|
||||
protected override IEnumerable<IValidationRule> GetValidationRules(EmailField element) {
|
||||
var settings = element.ValidationSettings;
|
||||
|
||||
if (settings.IsRequired == true)
|
||||
yield return _validationRuleFactory.Create<Required>(settings.CustomValidationMessage);
|
||||
|
||||
if (settings.MaximumLength != null) {
|
||||
yield return _validationRuleFactory.Create<StringLength>(r => {
|
||||
r.Maximum = settings.MaximumLength;
|
||||
r.ErrorMessage = settings.CustomValidationMessage;
|
||||
});
|
||||
}
|
||||
|
||||
yield return _validationRuleFactory.Create<EmailAddress>(settings.CustomValidationMessage);
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(settings.CompareWith)) {
|
||||
yield return _validationRuleFactory.Create<Compare>(r => {
|
||||
r.TargetName = settings.CompareWith;
|
||||
r.ErrorMessage = settings.CustomValidationMessage;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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<CommonElementSettings>();
|
||||
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) {
|
||||
<div>
|
||||
<label for="@commonSettings.Id">@element.Label</label>
|
||||
@Html.Raw(tagBuilder.ToString(TagRenderMode.SelfClosing))
|
||||
</div>
|
||||
}
|
||||
else {
|
||||
@Html.Raw(tagBuilder.ToString(TagRenderMode.SelfClosing))
|
||||
}
|
||||
@@ -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<CommonElementSettings>();
|
||||
var tagBuilder = (OrchardTagBuilder)TagBuilderExtensions.AddCommonElementAttributes(new OrchardTagBuilder("input"), Model);
|
||||
|
||||
tagBuilder.AddCssClass("text");
|
||||
tagBuilder.Attributes["type"] = "email";
|
||||
tagBuilder.Attributes["name"] = element.Name;
|
||||
tagBuilder.AddClientValidationAttributes((IDictionary<string, string>)Model.ClientValidationAttributes);
|
||||
|
||||
if (!ViewData.ModelState.IsValidField(element.Name)) {
|
||||
tagBuilder.AddCssClass("input-validation-error");
|
||||
}
|
||||
}
|
||||
|
||||
@if (element.ShowLabel) {
|
||||
<label for="@commonSettings.Id">@element.Label</label>
|
||||
}
|
||||
@Html.Raw(tagBuilder.ToString(TagRenderMode.SelfClosing))
|
||||
@if (element.ValidationSettings.ShowValidationMessage == true) {
|
||||
@Html.ValidationMessage(element.Name)
|
||||
}
|
||||
Reference in New Issue
Block a user