Adding TextArea validation.

This commit is contained in:
Sipke Schoorstra
2014-11-21 17:47:32 -08:00
parent fe19427ee8
commit 06ebab41fe
6 changed files with 90 additions and 7 deletions

View File

@@ -13,11 +13,12 @@ namespace Orchard.DynamicForms.Drivers {
_tokenizer = tokenizer;
}
protected override IEnumerable<string> FormNames {
get {
yield return "AutoLabel";
yield return "TextArea";
}
protected override EditorResult OnBuildEditor(TextArea element, ElementEditorContext context) {
var autoLabelEditor = BuildForm(context, "AutoLabel");
var textAreaEditor = BuildForm(context, "TextArea");
var textAreaValidation = BuildForm(context, "TextAreaValidation", "Validation:10");
return Editor(context, autoLabelEditor, textAreaEditor, textAreaValidation);
}
protected override void DescribeForm(DescribeContext context) {
@@ -46,6 +47,44 @@ namespace Orchard.DynamicForms.Drivers {
return form;
});
context.Form("TextAreaValidation", factory => {
var shape = (dynamic)factory;
var form = shape.Fieldset(
Id: "TextAreaValidation",
_IsRequired: shape.Checkbox(
Id: "IsRequired",
Name: "IsRequired",
Title: "Required",
Value: "true",
Description: T("Check to make this text area a required field.")),
_MinimumLength: shape.Textbox(
Id: "MinimumLength",
Name: "MinimumLength",
Title: "Minimum Length",
Classes: new[] { "text", "medium", "tokenized" },
Description: T("The minimum length required.")),
_MaximumLength: shape.Textbox(
Id: "MaximumLength",
Name: "MaximumLength",
Title: "Maximum Length",
Classes: new[] { "text", "medium", "tokenized" },
Description: T("The maximum length allowed.")),
_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;
});
}
protected override void OnDisplaying(TextArea element, ElementDisplayContext context) {

View File

@@ -44,7 +44,7 @@ namespace Orchard.DynamicForms.Drivers {
Name: "IsRequired",
Title: "Required",
Value: "true",
Description: T("Tick this checkbox to make this text field a required field.")),
Description: T("Check to make this text field a required field.")),
_MinimumLength: shape.Textbox(
Id: "MinimumLength",
Name: "MinimumLength",

View File

@@ -1,4 +1,5 @@
using Orchard.Layouts.Helpers;
using Orchard.DynamicForms.Validators.Settings;
using Orchard.Layouts.Helpers;
namespace Orchard.DynamicForms.Elements {
public class TextArea : LabeledFormElement {
@@ -11,5 +12,9 @@ namespace Orchard.DynamicForms.Elements {
get { return State.Get("Columns").ToInt32(); }
set { State["Columns"] = value.ToString(); }
}
public TextAreaValidationSettings ValidationSettings {
get { return State.GetModel<TextAreaValidationSettings>(""); }
}
}
}

View File

@@ -187,6 +187,7 @@
<Compile Include="Validators\Settings\EmailFieldValidationSettings.cs" />
<Compile Include="Validators\Settings\PasswordFieldValidationSettings.cs" />
<Compile Include="Validators\Settings\ReCaptchaValidationSettings.cs" />
<Compile Include="Validators\Settings\TextAreaValidationSettings.cs" />
<Compile Include="Validators\Settings\TextFieldValidationSettings.cs" />
<Compile Include="Services\Models\ValidationSettingsBase.cs" />
<Compile Include="Services\Models\FormEventContext.cs" />
@@ -226,6 +227,7 @@
<Compile Include="Services\Models\ValidateInputContext.cs" />
<Compile Include="Validators\PasswordFieldValidator.cs" />
<Compile Include="Validators\ReCaptchaValidator.cs" />
<Compile Include="Validators\TextAreaValidator.cs" />
<Compile Include="Validators\TextFieldValidator.cs" />
<Compile Include="ViewModels\FieldValidationSettings.cs" />
<Compile Include="ViewModels\FormBindingSettings.cs" />

View File

@@ -0,0 +1,9 @@
using Orchard.DynamicForms.Services.Models;
namespace Orchard.DynamicForms.Validators.Settings {
public class TextAreaValidationSettings : ValidationSettingsBase {
public bool? IsRequired { get; set; }
public int? MinimumLength { get; set; }
public int? MaximumLength { get; set; }
}
}

View File

@@ -0,0 +1,28 @@
using System.Collections.Generic;
using Orchard.DynamicForms.Elements;
using Orchard.DynamicForms.Services;
using Orchard.DynamicForms.ValidationRules;
namespace Orchard.DynamicForms.Validators {
public class TextAreaValidator : ElementValidator<TextArea> {
private readonly IValidationRuleFactory _validationRuleFactory;
public TextAreaValidator(IValidationRuleFactory validationRuleFactory) {
_validationRuleFactory = validationRuleFactory;
}
protected override IEnumerable<IValidationRule> GetValidationRules(TextArea element) {
var settings = element.ValidationSettings;
if (settings.IsRequired == true)
yield return _validationRuleFactory.Create<Required>(settings.CustomValidationMessage);
if (settings.MinimumLength != null || settings.MaximumLength != null) {
yield return _validationRuleFactory.Create<StringLength>(r => {
r.Minimum = settings.MinimumLength;
r.Maximum = settings.MaximumLength;
r.ErrorMessage = settings.CustomValidationMessage;
});
}
}
}
}