Exemple #1
0
        /// <summary>
        /// Adds the error to model state correctly for a property so we can use it on the client side.
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="result"></param>
        /// <param name="parts">
        /// Each model state validation error has a name and in most cases this name is made up of parts which are delimited by a '.'
        /// </param>
        internal static void AddValidationError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
                                                ValidationResult result, params string[] parts)
        {
            // if there are assigned member names, we combine the member name with the owner name
            // so that we can try to match it up to a real field. otherwise, we assume that the
            // validation message is for the overall owner.
            // Owner = the component being validated, like a content property but could be just an HTML field on another editor

            var withNames      = false;
            var delimitedParts = string.Join(".", parts);

            foreach (var memberName in result.MemberNames)
            {
                modelState.TryAddModelError($"{delimitedParts}.{memberName}", result.ToString());
                withNames = true;
            }
            if (!withNames)
            {
                modelState.TryAddModelError($"{delimitedParts}", result.ToString());
            }
        }