working on U4-5875 - Updated RequiredManifestValueValidator to automatically check for empty json strings if the value type is JSON. Updates the TagsPropertyEditor to check for empty json in it's required validator. Updated the tags JS to always update the model value so if there's a server side validation, it gets cleared on change.
This commit is contained in:
@@ -21,6 +21,15 @@ namespace Umbraco.Core.PropertyEditors
|
||||
else
|
||||
{
|
||||
var asString = value.ToString();
|
||||
|
||||
if (editor.ValueEditor.ValueType.InvariantEquals("JSON"))
|
||||
{
|
||||
if (asString.DetectIsEmptyJson())
|
||||
{
|
||||
yield return new ValidationResult("Value cannot be empty", new[] { "value" });
|
||||
}
|
||||
}
|
||||
|
||||
if (asString.IsNullOrWhiteSpace())
|
||||
{
|
||||
yield return new ValidationResult("Value cannot be empty", new[] { "value" });
|
||||
|
||||
@@ -34,6 +34,9 @@ angular.module("umbraco")
|
||||
if (tagToAdd.length > 0) {
|
||||
if ($scope.currentTags.indexOf(tagToAdd) < 0) {
|
||||
$scope.currentTags.push(tagToAdd);
|
||||
//update the model value, this is required if there's a server validation error, it can
|
||||
// only then be cleared if the model changes
|
||||
$scope.model.value = $scope.currentTags;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,6 +69,9 @@ angular.module("umbraco")
|
||||
var i = $scope.currentTags.indexOf(tag);
|
||||
if (i >= 0) {
|
||||
$scope.currentTags.splice(i, 1);
|
||||
//update the model value, this is required if there's a server validation error, it can
|
||||
// only then be cleared if the model changes
|
||||
$scope.model.value = $scope.currentTags;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -61,6 +62,61 @@ namespace Umbraco.Web.PropertyEditors
|
||||
var json = editorValue.Value as JArray;
|
||||
return json == null ? null : json.Select(x => x.Value<string>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the validator used for the required field validation which is specified on the PropertyType
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This will become legacy as soon as we implement overridable pre-values.
|
||||
///
|
||||
/// The default validator used is the RequiredValueValidator but this can be overridden by property editors
|
||||
/// if they need to do some custom validation, or if the value being validated is a json object.
|
||||
/// </remarks>
|
||||
internal override ManifestValueValidator RequiredValidator
|
||||
{
|
||||
get { return new RequiredTagsValueValidator(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Custom validator to validate a required value against an empty json value
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is required because the Tags property editor is not of type 'JSON', it's just string so the underlying
|
||||
/// validator does not validate against an empty json string
|
||||
/// </remarks>
|
||||
[ValueValidator("Required")]
|
||||
private class RequiredTagsValueValidator : ManifestValueValidator
|
||||
{
|
||||
/// <summary>
|
||||
/// Validates a null value or an empty json value
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="preValues"></param>
|
||||
/// <param name="editor"></param>
|
||||
/// <returns></returns>
|
||||
public override IEnumerable<ValidationResult> Validate(object value, string config, PreValueCollection preValues, PropertyEditor editor)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
yield return new ValidationResult("Value cannot be null", new[] { "value" });
|
||||
}
|
||||
else
|
||||
{
|
||||
var asString = value.ToString();
|
||||
|
||||
if (asString.DetectIsEmptyJson())
|
||||
{
|
||||
yield return new ValidationResult("Value cannot be empty", new[] { "value" });
|
||||
}
|
||||
|
||||
if (asString.IsNullOrWhiteSpace())
|
||||
{
|
||||
yield return new ValidationResult("Value cannot be empty", new[] { "value" });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class TagPreValueEditor : PreValueEditor
|
||||
|
||||
Reference in New Issue
Block a user