Skip to content

Commit

Permalink
feat(templates): add Validation Rule
Browse files Browse the repository at this point in the history
  • Loading branch information
seangwright committed Jul 8, 2023
1 parent f7b348b commit 0f60cbf
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ This package includes the following item templates
- [Admin Create Page](https://docs.xperience.io/xp/developers-and-admins/customization/extend-the-administration-interface/ui-pages/reference-ui-page-templates/edit-ui-page-template#EditUIpagetemplate-Useeditpagestocreatenewobjects)
- [Admin Page Extender](https://docs.xperience.io/x/4gSiCQ)
- [UI Form Component](https://docs.xperience.io/x/5ASiCQ)
- [Validation Rule](https://docs.xperience.io/x/6QSiCQ)
- [Validation Rule (TypeScript)](https://docs.xperience.io/xp/developers-and-admins/customization/extend-the-administration-interface/ui-form-components/ui-form-component-validation-rules#UIformcomponentvalidationrules-Validationrulefrontend)

### Form Builder

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json.schemastore.org/dotnetcli.host",
"usageExamples": [],
"symbolInfo": {}
}
6 changes: 6 additions & 0 deletions src/templates/validationRule/.template.config/ide.host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json.schemastore.org/ide.host",
"icon": "../../images/icon.png",
"defaultItemExtension": "cs",
"itemHierarchyPaths": ["Xperience by Kentico"]
}
86 changes: 86 additions & 0 deletions src/templates/validationRule/.template.config/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Xperience Community",
"classifications": ["Web", "ASP.NET"],
"name": "Xperience Validation Rule",
"generatorVersions": "[1.0.0.0-*)",
"description": "Xperience by Kentico Validation Rule with Properties, Client Properties, and Attribute",
"tags": {
"language": "C#",
"type": "item"
},
"groupIdentity": "XperinceCommunity.Admin.ValidationRule",
"precedence": "10",
"identity": "XperienceCommunity.Admin.ValidationRule.26.3.0.0",
"shortName": "xpc-validation-rule",
"sourceName": "NewValidationRule",
"primaryOutputs": [
{
"path": "NewValidationRule.cs"
}
],
"defaultName": "NewValidationRule",
"preferNameDirectory": false,
"preferDefaultName": true,
"symbols": {
"namespace": {
"type": "generated",
"generator": "coalesce",
"replaces": "MySite.Admin.Validation",
"parameters": {
"sourceVariableName": "IDENamespace",
"fallbackVariableName": "RootNamespace",
"defaultValue": "MySite.Admin.Validation"
}
},
"IDENamespace": {
"type": "bind",
"binding": "host:namespace",
"defaultValue": ""
},
"RootNamespace": {
"type": "bind",
"binding": "msbuild:RootNamespace",
"defaultValue": ""
},
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
},
"DisplayName": {
"type": "derived",
"valueSource": "name",
"replaces": "DisplayName",
"valueTransform": "nameToWords"
}
},
"forms": {
"extractName": {
"identifier": "replace",
"pattern": "^(\\w+)ValidationRule$",
"replacement": "$1"
},
"pascalToWords": {
"identifier": "replace",
"pattern": "(?<!^)(?=[A-Z])",
"replacement": " "
},
"nameToWords": {
"identifier": "chain",
"steps": ["extractName", "pascalToWords"]
}
},
"postActions": [
{
"id": "openInEditor",
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")",
"description": "Opens the created Validation Rule in the editor",
"manualInstructions": [],
"actionId": "84C0DA21-51C8-4541-9940-6CA19AF04EE6",
"args": {
"files": "0"
},
"continueOnError": true
}
]
}
74 changes: 74 additions & 0 deletions src/templates/validationRule/NewValidationRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Kentico.Xperience.Admin.Base.Forms;
using Kentico.Xperience.Admin.Base.FormAnnotations;
using MySite.Admin.Validation;
using System.Threading.Tasks;
using CMS.Core;
using System;

[assembly: RegisterFormValidationRule(
identifier: NewValidationRule.IDENTIFIER,
ruleType: typeof(NewValidationRule),
name: "DisplayName",
description: "A DisplayName Validation Rule.")]

namespace MySite.Admin.Validation;

[ValidationRuleAttribute(typeof(NewValidationRuleAttribute))]
public class NewValidationRule : ValidationRule<NewValidationRuleProperties, NewValidationRuleClientProperties, string>
{
public const string IDENTIFIER = "MySite.Admin.Validation.NewValidationRule";

private readonly ILocalizationService localizer;

public override string ClientRuleName => "@MySite.Admin/NewValidationRule";

public NewValidationRule(ILocalizationService localizer)
{
this.localizer = localizer;
}

public override Task<ValidationResult> Validate(string value, IFormFieldValueProvider formFieldValueProvider)
{
if (Random.Shared.Next() > 10)
{
return ValidationResult.FailResult(localizer.LocalizeString("Please enter a valid value."));
}

return ValidationResult.SuccessResult();
}

protected override Task ConfigureClientProperties(NewValidationRuleClientProperties clientProperties)
{
return base.ConfigureClientProperties(clientProperties);
}
}

public class NewValidationRuleProperties : ValidationRuleProperties
{
[TextInputComponent(
Label = "Value",
ExplanationText = "A value to validate against",
Tooltip = "Enter a value",
Order = 1)]
public string Value { get; set; }

public override string GetDescriptionText(ILocalizationService localizationService)
{
return "";
}
}

public class NewValidationRuleClientProperties : ValidationRuleClientProperties
{
public string Value { get; set; }
}

public class NewValidationRuleAttribute : ValidationRuleAttribute
{
public NewValidationRuleAttribute(string value)
{
Value = value;
}

public string Value { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json.schemastore.org/dotnetcli.host",
"usageExamples": [],
"symbolInfo": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json.schemastore.org/ide.host",
"icon": "../../images/icon.png",
"defaultItemExtension": "cs",
"itemHierarchyPaths": ["Xperience by Kentico"]
}
60 changes: 60 additions & 0 deletions src/templates/validationRuleClient/.template.config/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Xperience Community",
"classifications": ["Web", "TypeScript"],
"name": "Xperience Validation Rule (TypeScript)",
"generatorVersions": "[1.0.0.0-*)",
"description": "Xperience by Kentico Validation Rule Front end validation function in TypeScript",
"tags": {
"language": "TypeScript",
"type": "item"
},
"groupIdentity": "XperinceCommunity.Admin.ValidationRule",
"precedence": "10",
"identity": "XperienceCommunity.Admin.ValidationRuleClient.26.3.0.0",
"shortName": "xpc-validation-rule-client",
"sourceName": "NewValidationRule",
"primaryOutputs": [
{
"path": "NewValidationRule.ts"
}
],
"defaultName": "NewValidationRule",
"preferNameDirectory": false,
"preferDefaultName": true,
"symbols": {
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
}
},
"forms": {
"extractName": {
"identifier": "replace",
"pattern": "^(\\w+)ValidationRule$",
"replacement": "$1"
},
"pascalToWords": {
"identifier": "replace",
"pattern": "(?<!^)(?=[A-Z])",
"replacement": " "
},
"nameToWords": {
"identifier": "chain",
"steps": ["extractName", "pascalToWords"]
}
},
"postActions": [
{
"id": "openInEditor",
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")",
"description": "Opens the created Validation Rule in the editor",
"manualInstructions": [],
"actionId": "84C0DA21-51C8-4541-9940-6CA19AF04EE6",
"args": {
"files": "0"
},
"continueOnError": true
}
]
}
15 changes: 15 additions & 0 deletions src/templates/validationRuleClient/NewValidationRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
ValidationRule,
ValidationRuleProps,
} from "@kentico/xperience-admin-base";

interface NewValidationRuleProps extends ValidationRuleProps {
readonly value: string;
}

export const NewValidationRule: ValidationRule<
NewValidationRuleProps,
string
> = (props, value) => {
return { isValid: false, errorMessage: props.errorMessage };
};

0 comments on commit 0f60cbf

Please sign in to comment.