PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/web/mcs/class/System.Web.Mvc2/System.Web.Mvc/DataErrorInfoModelValidatorProvider.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 87 lines | 62 code | 10 blank | 15 comment | 13 complexity | 3e7efe8665be95315e961a10718e74d8 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. *
  5. * This software is subject to the Microsoft Public License (Ms-PL).
  6. * A copy of the license can be found in the license.htm file included
  7. * in this distribution.
  8. *
  9. * You must not remove this notice, or any other, from this software.
  10. *
  11. * ***************************************************************************/
  12. namespace System.Web.Mvc {
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel;
  16. using System.Linq;
  17. public class DataErrorInfoModelValidatorProvider : ModelValidatorProvider {
  18. public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context) {
  19. if (metadata == null) {
  20. throw new ArgumentNullException("metadata");
  21. }
  22. if (context == null) {
  23. throw new ArgumentNullException("context");
  24. }
  25. return GetValidatorsImpl(metadata, context);
  26. }
  27. private static IEnumerable<ModelValidator> GetValidatorsImpl(ModelMetadata metadata, ControllerContext context) {
  28. // If the metadata describes a model that implements IDataErrorInfo, we should call its
  29. // Error property at the appropriate time.
  30. if (TypeImplementsIDataErrorInfo(metadata.ModelType)) {
  31. yield return new DataErrorInfoClassModelValidator(metadata, context);
  32. }
  33. // If the metadata describes a property of a container that implements IDataErrorInfo,
  34. // we should call its Item indexer at the appropriate time.
  35. if (TypeImplementsIDataErrorInfo(metadata.ContainerType)) {
  36. yield return new DataErrorInfoPropertyModelValidator(metadata, context);
  37. }
  38. }
  39. private static bool TypeImplementsIDataErrorInfo(Type type) {
  40. return typeof(IDataErrorInfo).IsAssignableFrom(type);
  41. }
  42. internal sealed class DataErrorInfoClassModelValidator : ModelValidator {
  43. public DataErrorInfoClassModelValidator(ModelMetadata metadata, ControllerContext controllerContext)
  44. : base(metadata, controllerContext) {
  45. }
  46. public override IEnumerable<ModelValidationResult> Validate(object container) {
  47. IDataErrorInfo castModel = Metadata.Model as IDataErrorInfo;
  48. if (castModel != null) {
  49. string errorMessage = castModel.Error;
  50. if (!String.IsNullOrEmpty(errorMessage)) {
  51. return new ModelValidationResult[] {
  52. new ModelValidationResult() { Message = errorMessage }
  53. };
  54. }
  55. }
  56. return Enumerable.Empty<ModelValidationResult>();
  57. }
  58. }
  59. internal sealed class DataErrorInfoPropertyModelValidator : ModelValidator {
  60. public DataErrorInfoPropertyModelValidator(ModelMetadata metadata, ControllerContext controllerContext)
  61. : base(metadata, controllerContext) {
  62. }
  63. public override IEnumerable<ModelValidationResult> Validate(object container) {
  64. IDataErrorInfo castContainer = container as IDataErrorInfo;
  65. if (castContainer != null && !String.Equals(Metadata.PropertyName, "error", StringComparison.OrdinalIgnoreCase)) {
  66. string errorMessage = castContainer[Metadata.PropertyName];
  67. if (!String.IsNullOrEmpty(errorMessage)) {
  68. return new ModelValidationResult[] {
  69. new ModelValidationResult() { Message = errorMessage }
  70. };
  71. }
  72. }
  73. return Enumerable.Empty<ModelValidationResult>();
  74. }
  75. }
  76. }
  77. }