Source/MarsRover.Web/Attributes/EnsureOneElementAttribute.cs
/// <summary>Validates the specified value with respect to the current validation attribute.</summary>
/// <returns>An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult" /> class. </returns>
/// <param name="value">The value to validate.</param>
/// <param name="validationContext">The context information about the validation operation.</param>
Validates the specified value with respect to the current validation attribute.
Validates the specified value with respect to the current validation attribute .
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            System.Diagnostics.Debug.WriteLine(string.Format("Running validation for {0}", value.GetType()));
            var list = value as IList;
            if (list != null)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("List has {0} elements and the minimum is {1}", list.Count, _minElements));
                if (list.Count >= _minElements)
                {
                    return ValidationResult.Success;
                }
                var memberNameList = new List<string>();
                if (validationContext.MemberName != null)
                {
                    memberNameList.Add(validationContext.MemberName);
                }
                if (!string.IsNullOrEmpty(this.ErrorMessage))
                {
                    return new ValidationResult(this.ErrorMessage, memberNameList);
                }
                else
                {
                    return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName), memberNameList);
                }
            }
            return base.IsValid(value, validationContext);
        }
protected override ValidationResult IsValid ( object value , ValidationContext validationContext ) { System . Diagnostics . Debug . WriteLine ( string . Format ( " " , value . GetType ( ) ) ) ; var list = value as IList ; if ( list != null ) { System . Diagnostics . Debug . WriteLine ( string . Format ( " " , list . Count , _minElements ) ) ; if ( list . Count >= _minElements ) { return ValidationResult . Success ; } var memberNameList = new List < string > ( ) ; if ( validationContext . MemberName != null ) { memberNameList . Add ( validationContext . MemberName ) ; } if ( ! string . IsNullOrEmpty ( this . ErrorMessage ) ) { return new ValidationResult ( this . ErrorMessage , memberNameList ) ; } else { return new ValidationResult ( this . FormatErrorMessage ( validationContext . DisplayName ) , memberNameList ) ; } } return base . IsValid ( value , validationContext ) ; }
Validates the specified value with respect to the current validation attribute.
Validates the specified value with respect to the current validation attribute .
An instance of the  class.
An instance of the class .
value
object
The value to validate.
The value to validate .
validationContext
ValidationContext
The context information about the validation operation.
The context information about the validation operation .