TextBox: Data-type validation for max chars (fixes #18817) (#20843)

* configure max chars for textbox

* min 1

* Adds server-side check for text box min and max character validation.

* Applied suggestion from code review.

* Bumped version of test helper

* Fixed test that was creating a text string data type with too large a maximum characters setting.

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>
Co-authored-by: Nhu Dinh <hnd@umbraco.dk>
This commit is contained in:
Niels Lyngsø
2025-11-18 08:16:59 +01:00
committed by GitHub
parent 92b5d11c95
commit c1b3f41f7c
7 changed files with 135 additions and 14 deletions

View File

@@ -1,7 +1,8 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.PropertyEditors.Validators;
namespace Umbraco.Cms.Core.PropertyEditors;
@@ -10,8 +11,17 @@ namespace Umbraco.Cms.Core.PropertyEditors;
/// </summary>
public class TextboxConfigurationEditor : ConfigurationEditor<TextboxConfiguration>
{
/// <summary>
/// Initializes a new instance of the <see cref="TextboxConfigurationEditor"/> class.
/// </summary>
public TextboxConfigurationEditor(IIOHelper ioHelper)
: base(ioHelper)
{
const int MinChars = 1;
const int MaxChars = 512;
Fields.Add(new ConfigurationField(new IntegerValidator(MinChars, MaxChars))
{
Key = "maxChars",
});
}
}

View File

@@ -5,19 +5,52 @@ using Umbraco.Extensions;
namespace Umbraco.Cms.Core.PropertyEditors.Validators;
/// <summary>
/// A validator that validates that the value is a valid integer
/// A validator that validates that the value is a valid integer.
/// </summary>
public sealed class IntegerValidator : IValueValidator
{
private readonly int? _minValue;
private readonly int? _maxValue;
/// <summary>
/// Initializes a new instance of the <see cref="IntegerValidator"/> class.
/// </summary>
public IntegerValidator()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="IntegerValidator"/> class with minimum and/or maximum values.
/// </summary>
public IntegerValidator(int? minValue, int? maxValue)
{
_minValue = minValue;
_maxValue = maxValue;
}
/// <inheritdoc />
public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration, PropertyValidationContext validationContext)
{
if (value != null && value.ToString() != string.Empty)
if (value == null || value.ToString() == string.Empty)
{
Attempt<int> result = value.TryConvertTo<int>();
if (result.Success == false)
yield break;
}
Attempt<int> result = value.TryConvertTo<int>();
if (result.Success == false)
{
yield return new ValidationResult("The value " + value + " is not a valid integer", ["value"]);
}
else
{
if (_minValue.HasValue && result.Result < _minValue.Value)
{
yield return new ValidationResult("The value " + value + " is not a valid integer", new[] { "value" });
yield return new ValidationResult("The value " + value + " is less than the minimum allowed value of " + _minValue.Value, ["value"]);
}
if (_maxValue.HasValue && result.Result > _maxValue.Value)
{
yield return new ValidationResult("The value " + value + " is greater than the maximum allowed value of " + _maxValue.Value, ["value"]);
}
}
}