DynamicForms.FormValidation
0.1.0
dotnet add package DynamicForms.FormValidation --version 0.1.0
NuGet\Install-Package DynamicForms.FormValidation -Version 0.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DynamicForms.FormValidation" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DynamicForms.FormValidation" Version="0.1.0" />
<PackageReference Include="DynamicForms.FormValidation" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DynamicForms.FormValidation --version 0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DynamicForms.FormValidation, 0.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DynamicForms.FormValidation@0.1.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DynamicForms.FormValidation&version=0.1.0
#tool nuget:?package=DynamicForms.FormValidation&version=0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
DynamicForms.FormValidation
Server-side form validation library for .NET compatible with @moe1399/ngx-dynamic-forms. Use the same form configuration and validation rules on both client and server.
Installation
dotnet add package DynamicForms.FormValidation
Usage
using DynamicForms.FormValidation;
using DynamicForms.FormValidation.Models;
using System.Text.RegularExpressions;
// Register custom validators (same logic as Angular/Node.js)
ValidatorRegistry.Instance.Register("australianPhoneNumber", (value, _, _, _) =>
{
if (value == null) return true;
var phone = value.ToString()?.Replace(" ", "") ?? "";
return Regex.IsMatch(phone, @"^(\+61|0)[2-478]\d{8}$");
});
// Use the same form config as your Angular app
var formConfig = new FormConfig
{
Id = "contact-form",
Fields = new List<FormFieldConfig>
{
new FormFieldConfig
{
Name = "email",
Label = "Email",
Type = FieldType.Email,
Validations = new List<ValidationRule>
{
new ValidationRule { Type = ValidationRuleType.Required, Message = "Email is required" },
new ValidationRule { Type = ValidationRuleType.Email, Message = "Invalid email format" }
}
},
new FormFieldConfig
{
Name = "phone",
Label = "Phone",
Type = FieldType.Text,
Validations = new List<ValidationRule>
{
new ValidationRule
{
Type = ValidationRuleType.Custom,
CustomValidatorName = "australianPhoneNumber",
Message = "Invalid Australian phone number"
}
}
}
}
};
// Validate submitted form data
var formData = new Dictionary<string, object?>
{
{ "email", "test@example.com" },
{ "phone", "0412345678" }
};
var result = FormValidator.ValidateForm(formConfig, formData);
if (!result.Valid)
{
foreach (var error in result.Errors)
{
Console.WriteLine($"{error.Field}: {error.Message}");
}
}
API
FormValidator.ValidateForm(config, data)
Validates form data against a form configuration.
Parameters:
config: FormConfig- The form configuration (same structure as@moe1399/ngx-dynamic-forms)data: Dictionary<string, object?>- The form data to validate
Returns: ValidationResult
public class ValidationResult
{
public bool Valid { get; set; }
public List<FieldValidationError> Errors { get; set; }
}
public class FieldValidationError
{
public string Field { get; set; } // e.g., "email" or "contacts[0].phone"
public string Message { get; set; }
public string Rule { get; set; } // Validation type that failed
}
FormValidator.ValidateFieldValue(fieldConfig, value, formData?)
Validates a single field value.
Parameters:
fieldConfig: FormFieldConfig- The field configurationvalue: object?- The value to validateformData: Dictionary<string, object?>?- Optional full form data for contextual validation
Returns: ValidationResult
ValidatorRegistry.Instance
Singleton registry for custom validators.
// Register a single validator
ValidatorRegistry.Instance.Register("validatorName", (value, parameters, fieldConfig, formData) =>
{
return true; // or false
});
// Register multiple validators
ValidatorRegistry.Instance.RegisterAll(new Dictionary<string, CustomValidatorFn>
{
{ "validator1", (v, _, _, _) => true },
{ "validator2", (v, _, _, _) => true }
});
// Check if validator exists
ValidatorRegistry.Instance.Has("validatorName");
// List all registered validators
ValidatorRegistry.Instance.List();
// Remove a validator
ValidatorRegistry.Instance.Unregister("validatorName");
// Clear all validators
ValidatorRegistry.Instance.Clear();
Built-in Validators
| Type | Value | Behavior |
|---|---|---|
required |
- | Non-empty value |
email |
- | Valid email format |
minLength |
number | String length >= value |
maxLength |
number | String length ⇐ value |
min |
number | Numeric value >= value |
max |
number | Numeric value ⇐ value |
pattern |
regex | Matches regex pattern |
custom |
- | Uses customValidatorName |
ASP.NET Core Example
using DynamicForms.FormValidation;
using DynamicForms.FormValidation.Models;
using System.Text.Json;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Register validators at startup
ValidatorRegistry.Instance.Register("australianPhoneNumber", (value, _, _, _) =>
{
if (value == null) return true;
var phone = value.ToString()?.Replace(" ", "") ?? "";
return Regex.IsMatch(phone, @"^(\+61|0)[2-478]\d{8}$");
});
// Form config (could be loaded from database)
var formConfig = new FormConfig { /* ... */ };
app.MapPost("/api/submit", async (HttpContext context) =>
{
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
var data = JsonSerializer.Deserialize<Dictionary<string, object?>>(body)
?? new Dictionary<string, object?>();
var result = FormValidator.ValidateForm(formConfig, data);
if (!result.Valid)
{
return Results.BadRequest(new { errors = result.Errors });
}
// Process valid form data
return Results.Ok(new { success = true });
});
app.Run();
JSON Deserialization
The library works with System.Text.Json for deserializing form configurations:
var json = @"{
""id"": ""my-form"",
""fields"": [
{
""name"": ""email"",
""label"": ""Email"",
""type"": ""email"",
""validations"": [
{ ""type"": ""required"", ""message"": ""Email is required"" }
]
}
]
}";
var config = JsonSerializer.Deserialize<FormConfig>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
var result = FormValidator.ValidateForm(config!, formData);
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- System.Text.Json (>= 8.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 131 | 1/8/2026 |