Files
Umbraco-CMS/src/Umbraco.Core/PropertyEditors/IntegerValidator.cs
perploug b6bcdcb351 fixes: U4-3728 Non-Mandatory Numeric property gives error when left blank
also fixes dimming on buttons fails when validation fails
2013-12-05 08:32:53 +01:00

30 lines
1.1 KiB
C#

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Umbraco.Core.Models;
namespace Umbraco.Core.PropertyEditors
{
/// <summary>
/// A validator that validates that the value is a valid integer
/// </summary>
[ValueValidator("Integer")]
internal sealed class IntegerValidator : ManifestValueValidator, IPropertyValidator
{
public override IEnumerable<ValidationResult> Validate(object value, string config, PreValueCollection preValues, PropertyEditor editor)
{
if (value != null && value.ToString() != string.Empty)
{
var result = value.TryConvertTo<int>();
if (result.Success == false)
{
yield return new ValidationResult("The value " + value + " is not a valid integer", new[] { "value" });
}
}
}
public IEnumerable<ValidationResult> Validate(object value, PreValueCollection preValues, PropertyEditor editor)
{
return Validate(value, "", preValues, editor);
}
}
}